首页 > Windows开发 > 详细

Scraping JavaScript webpages with webkit | WebScraping.com

时间:2014-03-04 20:55:39      阅读:518      评论:0      收藏:0      [点我收藏+]

Scraping JavaScript webpages with webkit | WebScraping.com

Scraping JavaScript webpages with webkit
Posted 12 Mar 2010 in javascript, python, qt, and webkit

 

In the previous post I covered how to tackle JavaScript based websites with Chickenfoot. Chickenfoot is great but not perfect because it:

  1. requires me to program in JavaScript rather than my beloved Python (with all its great libraries)
  2. is slow because have to wait for FireFox to render the entire webpage
  3. is somewhat buggy and has a small user/developer community, mostly at MIT

An alternative solution that addresses all these points is webkit, the open source browser engine used most famously in Apple‘s Safari browser. Webkit has now been ported to the Qt framework and can be used through its Python bindings.

Here is a simple class that renders a webpage (including executing any JavaScript) and then saves the final HTML to a file:

import sys  
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
from PyQt4.QtWebKit import *  
  
class Render(QWebPage):  
  def __init__(self, url):  
    self.app = QApplication(sys.argv)  
    QWebPage.__init__(self)  
    self.loadFinished.connect(self._loadFinished)  
    self.mainFrame().load(QUrl(url))  
    self.app.exec_()  
  
  def _loadFinished(self, result):  
    self.frame = self.mainFrame()  
    self.app.quit()  
  
url = ‘http://webscraping.com‘  
r = Render(url)  
html = r.frame.toHtml()  

Scraping JavaScript webpages with webkit | WebScraping.com,布布扣,bubuko.com

Scraping JavaScript webpages with webkit | WebScraping.com

原文:http://www.cnblogs.com/lexus/p/3579917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!