標籤: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,此文問題通通解決