Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60256
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 202
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-26 15:34
个人简介

爱技术的大学狗

文章分类

全部博文(8)

文章存档

2014年(8)

我的朋友

分类: 大数据

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到手


阅读(4770) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~