1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| from PyQt4.QtCore import * from PyQt4.QtGui import * import sys import re import xlwt import xlrd import xlutils.copy import requests import time import os import traceback
class Downloader(QDialog):
def __init__(self): QDialog.__init__(self)
self.resize(750,300) layout = QGridLayout() self.infoTitle = QLabel("<center><b>数据获取设置</b></center>") self.title = QLabel("<center><b>输入网页URL</b></center>") self.url = QPlainTextEdit() self.save_location = QLineEdit() self.DB_location = QLineEdit() self.templete_location = QLineEdit() self.progress = QProgressBar() download = QPushButton("一键统计") browse = QPushButton("选择") DB_browse = QPushButton("选择") templete_browse = QPushButton("选择")
self.save_location.setPlaceholderText("文件保存路径") self.DB_location.setPlaceholderText("青年之声用户名信息存储文本") self.templete_location.setPlaceholderText("Excel模板文件")
self.progress.setValue(0) self.progress.setAlignment(Qt.AlignHCenter)
num = 1
layout.addWidget(self.infoTitle,num-1,0,1,4) layout.addWidget(self.DB_location,num,0,1,3) layout.addWidget(DB_browse,num,3,1,1) layout.addWidget(self.templete_location,num+1,0,1,3) layout.addWidget(templete_browse,num+1,3,1,1) layout.addWidget(self.title,num+2,0,1,4) layout.addWidget(self.url,num+3,0,1,4) layout.addWidget(self.save_location,num+4,0,1,3) layout.addWidget(browse,num+4,3,1,1) layout.addWidget(self.progress,num+5,0,1,4) layout.addWidget(download,num+6,0,1,4)
self.setLayout(layout)
self.setWindowTitle("青年之声统计神器 - 制作者:16级数字媒体技术2班梁伟添") self.DB_location.setText("data.txt") self.templete_location.setText("templete.xls") self.setFocus()
download.clicked.connect(self.download) browse.clicked.connect(self.browse_file) DB_browse.clicked.connect(self.browse_DB) templete_browse.clicked.connect(self.browse_templete)
def browse_file(self): save_file = QFileDialog.getSaveFileName(self, caption="保存文件到", directory=".",filter="Excel (*.xls)") self.save_location.setText(QDir.toNativeSeparators(save_file))
def browse_DB(self): DB_file = QFileDialog.getOpenFileNames(self, caption="获取青年之声用户名信息", directory=".",filter="txt (*.txt)") if not DB_file: return self.DB_location.setText(QDir.toNativeSeparators(DB_file[0]))
def browse_templete(self): templete_file = QFileDialog.getOpenFileNames(self, caption="获取模板Excel文件", directory=".",filter="Excel (*.xls)") if not templete_file: return self.templete_location.setText(QDir.toNativeSeparators(templete_file[0]))
def download(self): url = self.url.toPlainText() save_location = self.save_location.text() DB_location = self.DB_location.text() templete_location = self.templete_location.text()
print("开始写入")
if DB_location == "": QMessageBox.warning(self, "Warning", "用户数据不能为空") return
try: with open(DB_location,'r') as f: info = f.read() except Exception: QMessageBox.warning(self, "Warning", "青年之声用户名信息获取失败\n检查文件路径是否正确") return reg = r'(.*)' studentReg = re.compile(reg) self.studentName = re.findall(studentReg,info) self.studentName = [x for x in self.studentName if x != '']
reg = r'(http.*)' urldReg = re.compile(reg) urlList = re.findall(urldReg,url)
if templete_location != "": try: rb = xlrd.open_workbook(templete_location,formatting_info=True) book = xlutils.copy.copy(rb) sheet = book.get_sheet(0) except Exception: QMessageBox.warning(self, "Warning", "Excel模板获取失败\n检查文件路径是否正确") return else: book = xlwt.Workbook(encoding='utf-8', style_compression=0) sheet = book.add_sheet('青年之声统计', cell_overwrite_ok=True)
font0 = xlwt.Font() font0.name = '微软雅黑' alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_CENTER alignment.vert = xlwt.Alignment.VERT_CENTER
style0 = xlwt.XFStyle() style0.font = font0 style0.alignment = alignment
try: az = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" column = 4 r = 0
for name in self.studentName: userClass = name.split('\t')[0] userNum = name.split('\t')[1] userName = name.split('\t')[2] sheet.write(r+2, 0, r+1 ,style0) sheet.write(r+2, 1, userClass ,style0) sheet.write(r+2, 2, userNum ,style0) sheet.write(r+2, 3, userName ,style0) r += 1
for originUrl in urlList: reg = r'quId=(.*?)&' quIdReg = re.compile(reg) quId = re.findall(quIdReg,originUrl)[0] url = "https://api.12355.net/pc/service/getReplysByQuestionId?quId=%s&page=1&rows=500" % quId askUrl = "https://api.12355.net/pc/service/getQuesDetail?quId=%s" % quId try: response = requests.get(url) html = response.text response = requests.get(askUrl) askHtml = response.text except Exception: traceback.print_exc() QMessageBox.warning(self, "Warning", "网络连接失败") return
if html == "": break
reg = r'"askTime":"(.*?)"' askReg = re.compile(reg) askList = re.findall(askReg,askHtml)
reg = r'"creatorName":"(.*?)"' nameReg = re.compile(reg) nameList = re.findall(nameReg,html)
reg = r'"replyContent":"(.*?)"' contentReg = re.compile(reg) contentList = re.findall(contentReg,html)
reg = r'"replyTime":"(.*?)"' timeReg = re.compile(reg) timeList = re.findall(timeReg,html)
row = 0 sheet.write(1, column, xlwt.Formula('HYPERLINK("%s";"问题%s")' % (originUrl,column-3)),style0) for name in self.studentName: index = 0
for creatorName in nameList: if name.split('\t')[2] == creatorName: if askList[0].split(' ')[0] == timeList[index].split(' ')[0]: reg = r"(?u)\w" textReg = re.compile(reg) textList = re.findall(textReg,contentList[index]) if len(textList) >= 5: sheet.write(row+2, column , "合格" ,style0) break
index += 1 row += 1
percent = (column-3) * 100 / len(urlList) self.progress.setValue(int(percent)) print(str(int(percent))+"%")
if column < 26: sheet.write(row+2, column , xlwt.Formula("COUNTIF(%s3:%s%s,\"合格\")" % (az[column],az[column],row+2)) ,style0) else: letter = "%s%s" % (az[int(column/26-1)],az[column%26]) sheet.write(row+2, column , xlwt.Formula("COUNTIF(%s3:%s%s,\"合格\")" % (letter,letter,row+2)) ,style0) column += 1 r = 0 column -= 1 sheet.write(r+1, column+1 , "合计" ,style0) if column < 26: for name in self.studentName: sheet.write(r+2, column+1 , xlwt.Formula("COUNTIF(%s%s:%s%s,\"合格\")" % (az[4],r+3,az[column],r+3)) ,style0) r += 1 sheet.write(r+2, column+1 , xlwt.Formula("SUM(%s%s:%s%s)" % (az[4],r+3,az[column],r+3)) ,style0) else: letter = "%s%s" % (az[int(column/26-1)],az[column%26]) for name in self.studentName: sheet.write(r+2, column+1 , xlwt.Formula("COUNTIF(%s%s:%s%s,\"合格\")" % (az[4],r+3,letter,r+3)) ,style0) r += 1 sheet.write(r+2, column+1 , xlwt.Formula("SUM(%s%s:%s%s)" % (az[4],r+3,letter,r+3)) ,style0) book.save(save_location) except Exception: traceback.print_exc() QMessageBox.warning(self, "Warning", "数据写入失败") return
print("完成写入") QMessageBox.information(self, "Information", "数据写入完成") self.progress.setValue(0)
app = QApplication(sys.argv) dl = Downloader() dl.show() app.exec_()
|