pylibcurl之https搜尋引擎之網路資料抓取小例子,302moved?google搜尋引擎不讓你抓搜尋結果??ok,此文問題通通解決

來源:互聯網
上載者:User

標籤:libcurl   google   搜尋引擎   

前提:操作平台-WIN7


一.首先你得python,我安裝的是python2.7.9


二.其次,你得安裝pyLibCurl,安裝方式:http://pycurl.sourceforge.net/


三.最後,你得編寫測試案例test.py:(當然,從代碼中可以看出你電腦得有E盤,否則改一下代碼,然後我抓取的資料是google一下test的資料)

#! /usr/bin/env python# -*- coding: utf-8 -*-# vi:ts=4:etimport sysimport pycurlclass Test:    def __init__(self):        self.contents = ''    def body_callback(self, buf):        self.contents = self.contents + buf        f = open('e:\\tmp\\tmp.html', 'w')        print f        f.write(self.contents)sys.stderr.write("Testing %s\n" % pycurl.version)t = Test()c = pycurl.Curl()c.setopt(c.URL, 'https://www.google.com/search?q=testx')c.setopt(c.USERAGENT, "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) in my heart of heart.")c.setopt(c.HEADER, True)c.setopt(c.REFERER, "https://www.google.com/search?q=testx")c.setopt(c.COOKIEFILE, "./COOKIE.txt")c.setopt(c.COOKIEJAR, "./COOKIE.txt")c.setopt(c.WRITEFUNCTION, t.body_callback)c.perform()c.close()print(t.contents)



延伸:

是不是你正常發起http請求google會告訴你“302 Moved”,ok,仔細研究一下這段代碼,也會解決你的問題


參考:

http://superuser.com/questions/482470/google-302-moved-in-firefox

http://stackoverflow.com/questions/22570970/php-search-by-image-google-curl-return-302-moved




pylibcurl之https搜尋引擎之網路資料抓取小例子,302moved?google搜尋引擎不讓你抓搜尋結果??ok,此文問題通通解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.