设为首页 收藏本站
查看: 898|回复: 0

[经验分享] python网页抓取之自己动手写字典

[复制链接]

尚未签到

发表于 2015-4-23 08:40:16 | 显示全部楼层 |阅读模式
  由于上篇的是在命令行中获取翻译的,方便性肯定一般啦。最多算个
  好玩些罢了。周末没事,就把上篇的代码搬进基于pyqt4中的图形软件中了。
  上篇代码当作模块使用不方便,做了更改,另外,两点注意:
  1.qt支持html的标签对于字符串的修饰。
  比如:string 显示的是红色的string。
  2.存储在数据库中存储翻译时使用的'\n'换行符如果使用标签修饰就会失效,
  只能在存储时换成来分割不同的翻译,当然如果你不用这种方法显示
  字符串就不需要考虑了,下图字符串是用标签修饰后的效果。
  
DSC0000.png
  本人自己的电脑装的是linux mint,默认窗体就是这个样子,透明度是0.9。
  OK按钮翻译输入框的单词(快捷键回车),clip按钮翻译剪切板的单词(快捷键
  是shift),如果不是单词,程序会过滤掉。
  鼠标离开主窗体(快捷键alt可以直接隐藏主窗体)仅显示:
DSC0001.png
  双击它唤出主窗体(快捷键:x)。
  实现的窗体代码如下,获取翻译模块部分可以去:
  https://github.com/915546302/dictionary


DSC0002.gif DSC0003.gif


  1 #!/usr/bin/python
  2 #--*--coding:utf-8--*--
  3
  4 import sys,re
  5 from PyQt4 import QtGui
  6 from PyQt4 import QtCore
  7 from dic import Fecth
  8 import time
  9 class IrregularForm(QtGui.QWidget):
10         def __init__(self, parent=None):
11             QtGui.QWidget.__init__(self, parent)
12             self.parent=parent
13
14             mask=QtGui.QPixmap("./icons/search40.png")
15             self.setMask(QtGui.QBitmap(mask.mask()))
16             p=QtGui.QPalette()
17             p.setBrush(QtGui.QPalette.Window, QtGui.QBrush(mask))
18             self.setPalette(p)
19             self.setGeometry(100, 100, 100, 100)
20             self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
21             self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
22             self.setWindowIcon(QtGui.QIcon('./icons/search50.png'))
23             self.mouseMovePos = QtCore.QPoint(0, 0)
24         def mouseMoveEvent(self,event):
25              if(self.mouseMovePos != QtCore.QPoint(0, 0)):
26                 self.move(self.geometry().x() + event.globalPos().x() \
27                     - self.mouseMovePos.x(),self.geometry().y() \
28                     + event.globalPos().y() - self.mouseMovePos.y())
29                 self.mouseMovePos = event.globalPos()
30         def mousePressEvent(self,event):
31             self.mouseMovePos = event.globalPos()
32
33         def mouseReleaseEvent(self,event):
34             self.mouseMovePos = QtCore.QPoint(0, 0)
35         def mouseDoubleClickEvent(self, event):
36             self.emit(QtCore.SIGNAL('trueVisible()'))
37         def keyPressEvent(self, event):
38             if event.key() == QtCore.Qt.Key_X:
39                 self.emit(QtCore.SIGNAL('trueVisible()'))
40 class Dic(QtGui.QWidget):
41     def __init__(self, parent=None):
42         QtGui.QWidget.__init__(self, parent)
43         self.alt=None
44         self.irregular=IrregularForm()
45         self.irregular.show()
46  
47         self.curTime=time.strftime("%Y-%m-%d %H:%M", \
48             time.localtime(time.time()))
49         okButton = QtGui.QPushButton("OK")
50         cbbtn = QtGui.QPushButton("clip")
51
52         self.edit = QtGui.QLineEdit()
53         self.word = QtGui.QLabel( self.curTime)
54         rem = QtGui.QLabel('Word:')
55         self.edit.setStyleSheet("color: blue;"
56                          "selection-color: yellow;"
57                          "selection-background-color: blue;")
58         #cbbtn.setStyleSheet("color:red");
59         cbbtn.setStyleSheet("color: blue;border:2px groove gray;"
60                           "border-radius:10px;padding:2px 4px;")
61         okButton.setStyleSheet("border:2px groove gray;"
62                           "border-radius:10px;padding:2px 4px;")
63
64         hbox = QtGui.QHBoxLayout()
65         hbox.addWidget(rem)
66         hbox.addWidget(self.edit)
67         hbox.addWidget(okButton)
68         hbox.addWidget(cbbtn)
69
70         vbox = QtGui.QVBoxLayout()
71         vbox.addLayout(hbox)
72         vbox.addWidget(self.word)
73         
74         self.setLayout(vbox)
75         self.setWindowOpacity(0.95)
76         self.setWindowTitle('Dictionary')
77         self.setGeometry(300, 300, 350, 150)
78         self.setWindowIcon(QtGui.QIcon('./icons/search50.png'))
79         self.connect(okButton, QtCore.SIGNAL('clicked()'), \
80             self.okButton)
81         self.connect(cbbtn, QtCore.SIGNAL('clicked()'), \
82             self.clipboardBotton)
83         self.connect(self.irregular, QtCore.SIGNAL('trueVisible()'), \
84             self,QtCore.SLOT('trueVisible()') )
85         #self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
86         self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
87         #self.fe=Fecth()
88     @QtCore.pyqtSlot()
89     def trueVisible(self):
90         self.setVisible(True)
91         self.irregular.setVisible(False)
92
93     def clipboardBotton(self):
94         clipboard = QtGui.QApplication.clipboard()
95         print clipboard.text()
96         self.edit.setText(clipboard.text())
97         self.buttonClicked(clipboard.text())
98     def okButton(self):
99         src=self.edit.text()
100         self.buttonClicked(src)
101     def setColor(self,string,key):
102         if key=='red':
103             return ''+string+''+''
104         elif key=='blue':
105             return ''+string+''+''
106         else:
107             return ''+string+''+''
108     def buttonClicked(self,src):
109
110         g=re.match('[a-zA-Z]+',src)
111         if not g:
112             self.word.setText(self.setColor('Not a word!','red'))
113             return
114         src=g.group()
115         self.fe=Fecth()
116         rows=self.fe.searchDB(src,None)
117         trans=''
118         if rows:
119             for row in rows:
120                 trans = self.setColor(row[0],'red')
121                 if row[1]!='':
122                    trans+=self.setColor(row[1]+','+row[2],'blue')
123                 trans+= self.setColor(row[3],'green')
124             self.word.setText(''+trans+'')
125         else:
126            
127             trans=self.setColor(src,'red')
128             zh=self.fe.fecth('qt')
129             print zh[0],zh[1]
130             trans+=self.setColor(zh[0],'blue')
131             tmp=zh[1]
132             if tmp=='':
133                 tmp='Fetch fail!'
134             trans+= self.setColor(tmp,'green')
135             self.word.setText(''+trans+'')
136         self.fe.close()
137     def keyReleaseEvent(self, event):
138            
139             if event.key() == QtCore.Qt.Key_Return:
140                 self.okButton()
141             elif event.key() == QtCore.Qt.Key_Shift:
142                 self.clipboardBotton()
143             elif event.key() == QtCore.Qt.Key_Alt:
144                 self.alt=QtCore.Qt.Key_Alt
145                 self.setVisible(False)
146                 self.irregular.setVisible(True)
147             elif event.key() == QtCore.Qt.Key_Space:
148                 self.edit.setText('')
149     def closeEvent(self, event):
150         
151         QtGui.qApp.quit()
152         #self.irregular.destroy()
153     def leaveEvent(self,evt):
154         if self.alt==QtCore.Qt.Key_Alt:
155             self.alt=-1
156             return
157         cx,cy=QtGui.QCursor.pos().x(),QtGui.QCursor.pos().y()
158         if(cx >= self.x() and cx = self.y() and cy

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59824-1-1.html 上篇帖子: Python自然语言处理学习笔记(1):目录 下篇帖子: python 简单谈谈“类”
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表