用python搜google

昨天上网查了一下资料,终于可以用python下载google的搜索页了。
如果直接用urllib.urlopen(url)会返回一个forbidden的,其实加一个add_header就可以了.
给出一个关键字
word 就会返回搜索结果了。

import urllib
import urllib2
def search(word):
 url=r'http://www.google.cn/search?hl=zh-CN&newwindow=1&q='+word+"&start=10&sa=N"
 req=urllib2.Request(url)
 req.add_header("User-Agent",'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322)')
 opener=urllib2.build_opener()
 text=opener.open(req).read()
 return text

你可能感兴趣的:(windows,.net,python,Google,百度)