QtWebKit解析js页面

来源:互联网 发布:一块钱能买什么淘宝 编辑:程序博客网 时间:2024/05/17 03:50

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)

#encoding=utf-8import urllib, urlparseif __name__ == '__main__':        baseUrl = 'http://s.weibo.com/weibo/'        wordList = ['python','c++','钓鱼岛', '博圣云峰', '加勒比海盗', '海贼王', '2012', '世界末日', '地球']        for index in range(len(wordList)):                url = urlparse.urljoin(baseUrl, urllib.quote(urllib.quote(wordList[index])))                print url                conn = urllib.urlopen(url)                data = conn.read()                f = file('/tmp/%s' % (wordList[index]), 'w')                f.write(data)                f.close()
2.用QtWebKit解析web页面(html+js)

#!/usr/bin/env python#encoding=utf-8import sys  from PyQt4.QtGui import *  from PyQt4.QtCore import *  from PyQt4.QtWebKit import *  import timeclass Render(QWebPage):    def __init__(self):     self.wordList = ['python','c++','钓鱼岛', '博圣云峰', '加勒比海盗', '海贼王', '2012', '世界末日', '地球']    self.index = 0    self.app = QApplication(sys.argv)      QWebPage.__init__(self)    self.loadFinished.connect(self._loadFinished)      self.mainFrame().setHtml(file('/tmp/%s'%self.wordList[self.index], 'r').read())    self.app.exec_()      def _loadFinished(self, result):      file('/home/dongsong/桌面/%s.html'%self.wordList[self.index],'w').write(unicode(self.mainFrame().toHtml()).encode('utf-8'))    self.index += 1    if self.index >= len(self.wordList):        self.app.quit()    else:        self.mainFrame().setHtml(file('/tmp/%s'%self.wordList[self.index], 'r').read())    page = Render()

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

export DISPLAY=:0vpython qt_load_2.py



原创粉丝点击