现在的位置: 首页 > 综合 > 正文

QtWebKit

2019年03月14日 ⁄ 综合 ⁄ 共 2101字 ⁄ 字号 评论关闭

WekKit官网:http://www.webkit.org/

QtWebKit官网及安装:http://trac.webkit.org/wiki/QtWebKit#GettingInvolved

QtWebKit Class Reference:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qtwebkit.html

QtWebKit也可以在PyQt4的安装中顺带安装(http://blog.csdn.net/xiarendeniao/article/details/6774520 46条)

1.用python的urllib库从服务端读取web页面(html+js)

[python] view
plain
copy

  1. #encoding=utf-8  
  2.   
  3. import urllib, urlparse  
  4.   
  5. if __name__ == '__main__':  
  6.         baseUrl = 'http://s.weibo.com/weibo/'  
  7.         wordList = ['python','c++','钓鱼岛''博圣云峰''加勒比海盗''海贼王''2012''世界末日''地球']  
  8.         for index in range(len(wordList)):  
  9.                 url = urlparse.urljoin(baseUrl, urllib.quote(urllib.quote(wordList[index])))  
  10.                 print url  
  11.                 conn = urllib.urlopen(url)  
  12.                 data = conn.read()  
  13.                 f = file('/tmp/%s' % (wordList[index]), 'w')  
  14.                 f.write(data)  
  15.                 f.close()  

2.用QtWebKit解析web页面(html+js)

[python] view
plain
copy

  1. #!/usr/bin/env python  
  2. #encoding=utf-8  
  3.   
  4. import sys    
  5. from PyQt4.QtGui import *    
  6. from PyQt4.QtCore import *    
  7. from PyQt4.QtWebKit import *    
  8. import time  
  9.   
  10. class Render(QWebPage):    
  11.   def __init__(self):   
  12.     self.wordList = ['python','c++','钓鱼岛''博圣云峰''加勒比海盗''海贼王''2012''世界末日''地球']  
  13.     self.index = 0  
  14.     self.app = QApplication(sys.argv)    
  15.     QWebPage.__init__(self)  
  16.     self.loadFinished.connect(self._loadFinished)    
  17.     self.mainFrame().setHtml(file('/tmp/%s'%self.wordList[self.index], 'r').read())  
  18.     self.app.exec_()    
  19.     
  20.   def _loadFinished(self, result):    
  21.     file('/home/dongsong/桌面/%s.html'%self.wordList[self.index],'w').write(unicode(self.mainFrame().toHtml()).encode('utf-8'))  
  22.     self.index += 1  
  23.     if self.index >= len(self.wordList):  
  24.         self.app.quit()  
  25.     else:  
  26.         self.mainFrame().setHtml(file('/tmp/%s'%self.wordList[self.index], 'r').read())  
  27.       
  28. page = Render()  


PS:可以用self.mainFrame().load(QUrl('http://s.weibo.com/python')直接访问页面并解析(html+js),上述示例只是为了演示如何解析已经读取到的pageData

[plain] view
plain
copy

  1. export DISPLAY=:0  
  2. vpython qt_load_2.py  


【上篇】
【下篇】

抱歉!评论已关闭.