请考虑以下Python代码:
30 url = "http://www.google.com/search?hl=en&safe=off&q=Monkey" 31 url_object = urllib.request.urlopen(url); 32 print(url_object.read());
运行此命令时,将抛出异常:
File "/usr/local/lib/python3.0/urllib/request.py", line 485, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden
但是,当将其放入浏览器时,搜索将按预期返回.这里发生了什么?我怎样才能克服这个问题,以便以编程方式搜索Google?
有什么想法吗?
这应该可以解决问题
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7' url = "http://www.google.com/search?hl=en&safe=off&q=Monkey" headers={'User-Agent':user_agent,} request=urllib2.Request(url,None,headers) //The assembled request response = urllib2.urlopen(request) data = response.read() // The data u need
如果您想通过编程界面"正确"进行Google搜索,请查看Google API.这些不仅是搜索Google的官方方式,如果Google更改其结果页面布局,它们也不太可能改变.