清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
# -*- coding: utf-8 -*
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import sys
import urllib.request
import re
def myformat(s):
while s.find('<') != -1:
i=s.find('<')
j=s.find('>')
s = s[:i]+s[j+1:]
while s.find('&') != -1:
i=s.find('&')
s = s[:i]+s[i+4:]
while s.find(' ') != -1:
i=s.find(' ')
s = s[:i]+s[i+5:]
while s.find('<') != -1:
s = s[:s.find('<')]+s[s.find('<')+len('<'):]
return s
def translate(word):
print(word)
url = 'http://dict.baidu.com/s?wd=' + word
print("url = " + url);
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
result = ""
re_soundmark = re.compile('英.*?<b lang="EN-US" xml:lang="EN-US">(.*?)</b>')
soundmark = re_soundmark.findall(html)
print(soundmark)
if not soundmark:
result += word + '\n'
else:
result += word + '\n英:' + soundmark[0]
re_soundmark = re.compile('美.*?<b lang="EN-US" xml:lang="EN-US">(.*?)</b>')
soundmark = re_soundmark.findall(html)
if not soundmark:
result += ' '.join(word) +'\n'
else:
result += " 美:" + soundmark[0] + '\n'
re_colines = re.compile('<span class="collins-cnmeans">(.*?)</span><span>(.*?)</span>')
colines = re_colines.findall(html)
for k, v in colines:
result += k+'\n'
result += myformat(v) + '\n'
re_meaning = re.compile('<p><strong>(.*?)</strong><span>(.*?)</span></p>')
meanings = re_meaning.findall(html)
for k ,v in meanings:
result += myformat(k) + myformat(v) +'\n'
re_statement = re.compile('<li><p>(.*?)</p><p>(.*?)</p></li>')
statements = re_statement.findall(html)
i=0
for k ,v in statements:
if i >= 5:
break
result += myformat(k)+myformat(v)+'\n'
i=i+1
return result
class WorkThread(QThread):
trigger = pyqtSignal(str)
def __init__(self):
super(WorkThread, self).__init__()
self.word = ""
def SetWord(self, Word):
self.word = Word
def run(self):
print("run\n")
print("world = {0}".format(self.word))
result = translate(self.word)
self.trigger.emit(result)
class MyWindow(QWidget):
def __init__(self):
super(MyWindow, self).__init__()
self.TextEdit = QTextEdit()
self.TextEdit.setFixedHeight(30)
self.button1 = QPushButton("One")
self.button2 = QPushButton("Search")
self.label1 = QLabel()
self.layout = QGridLayout()
self.layout.addWidget(self.TextEdit)
self.layout.addWidget(self.button1)
self.layout.addWidget(self.button2)
self.layout.addWidget(self.label1)
self.setLayout(self.layout)
self.resize(200,200)
self.button1.clicked.connect(self.ButtonCallBack)
self.button2.clicked.connect(self.StartWorkInAThread)
self.w = WorkThread()
self.w.trigger.connect(self.Stop)
def ButtonCallBack(self, e):
print(e)
print(type(self))
print(self.sender().text())
def StartWorkInAThread(self, e):
text = self.TextEdit.toPlainText()
self.w.SetWord(text)
self.w.start()
print("start work\n")
def Stop(self, text):
print("stop\n")
print(text)
print(self.sender().isFinished())
self.label1.setText(text)
if __name__=='__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())