分享

pywebkit 爬虫入门

 imelee 2017-03-07
pywebkit 爬虫入门 2014-05-10 23:49:39

分类: 大数据

最近在搞爬虫,然后得知webkit是个爬虫最终利器,然后开始去了解了一下pywebkit。
发现国内很少这方面的资源,国外的话搜寻了好久也没搜到比较好的。
官网上的文档貌似也没找着,然后看了下官网给的例子,结果还是没找着想要的。

然后东凑西拼了一下,找到了一个返回html的类,如下:

点击(此处)折叠或打开

  1. class WebView(webkit.WebView):
  2.     def get_html(self):
  3.         self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
  4.         html = self.get_main_frame().get_title()
  5.         self.execute_script('document.title=oldtitle;')
  6.         return html

然后自己弄了几个webview的对象出来,open了一下一个url,然后调用get_html()发现啥都木有

点击(此处)折叠或打开

  1. web = WebView()
  2. web.open(url)
  3. html = web.get_html()

  4. print html
  5. print str(html)

后来又google了一番,终于找到怎么调用这个类的方法:


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. import sys, threads # kudos to Nicholas Herriot (see comments)
  3. import gtk
  4. import webkit
  5. import warnings
  6. from time import sleep
  7. from optparse import OptionParser
  8.  
  9. warnings.filterwarnings('ignore')
  10.  
  11. class WebView(webkit.WebView):
  12.     def get_html(self):
  13.         self.execute_script('oldtitle=document.title;document.title=document.documentElement.innerHTML;')
  14.         html = self.get_main_frame().get_title()
  15.         self.execute_script('document.title=oldtitle;')
  16.         return html
  17.  
  18. class Crawler(gtk.Window):
  19.     def __init__(self, url, file):
  20.         gtk.gdk.threads_init() # suggested by Nicholas Herriot for Ubuntu Koala
  21.         gtk.Window.__init__(self)
  22.         self._url = url
  23.         self._file = file
  24.  
  25.     def crawl(self):
  26.         view = WebView()
  27.         view.open(self._url)
  28.         view.connect('load-finished', self._finished_loading)
  29.         self.add(view)
  30.         gtk.main()
  31.  
  32.     def _finished_loading(self, view, frame):
  33.         with open(self._file, 'w') as f:
  34.             f.write(view.get_html())
  35.         gtk.main_quit()
  36.  
  37. def main():
  38.     options = get_cmd_options()
  39.     crawler = Crawler(options.url, options.file)
  40.     crawler.crawl()
  41.  
  42. def get_cmd_options():
  43.     """
  44.         gets and validates the input from the command line
  45.     """
  46.     usage = "usage: %prog [options] args"
  47.     parser = OptionParser(usage)
  48.     parser.add_option('-u', '--url', dest = 'url', help = 'URL to fetch data from')
  49.     parser.add_option('-f', '--file', dest = 'file', help = 'Local file path to save data to')
  50.  
  51.     (options,args) = parser.parse_args()
  52.  
  53.     if not options.url:
  54.         print 'You must specify an URL.',sys.argv[0],'--help for more details'
  55.         exit(1)
  56.     if not options.file:
  57.         print 'You must specify a destination file.',sys.argv[0],'--help for more details'
  58.         exit(1)
  59.  
  60.     return options
  61.  
  62. if __name__ == '__main__':
  63.     main()
Ok,html到手

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多