乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      pywebkit 爬蟲入門

       imelee 2017-03-07
      pywebkit 爬蟲入門 2014-05-10 23:49:39

      分類: 大數(shù)據(jù)

      最近在搞爬蟲,然后得知webkit是個(gè)爬蟲最終利器,然后開始去了解了一下pywebkit。
      發(fā)現(xiàn)國內(nèi)很少這方面的資源,國外的話搜尋了好久也沒搜到比較好的。
      官網(wǎng)上的文檔貌似也沒找著,然后看了下官網(wǎng)給的例子,結(jié)果還是沒找著想要的。

      然后東湊西拼了一下,找到了一個(gè)返回html的類,如下:

      點(diǎn)擊(此處)折疊或打開

      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

      然后自己弄了幾個(gè)webview的對(duì)象出來,open了一下一個(gè)url,然后調(diào)用get_html()發(fā)現(xiàn)啥都木有

      點(diǎn)擊(此處)折疊或打開

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

      4. print html
      5. print str(html)

      后來又google了一番,終于找到怎么調(diào)用這個(gè)類的方法:


      點(diǎn)擊(此處)折疊或打開

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

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多