如何在Python中使用urllib或请求

时间:2016-07-20 14:37:18

标签: python python-3.x request urllib google-search

在Python 3中使用Google的正确方法是什么?我已尝试将requestsurllib用于Google页面。当我只是res = requests.get("https://www.google.com/#q=" + query)时没有使用与我在Safari中检查Google页面时相同的HTML。 urllib也是如此。当我使用Bing时会发生类似的事情。我熟悉AJAX。但是,现在似乎已经折旧了。

1 个答案:

答案 0 :(得分:0)

在python中,如果您没有手动在http请求中指定用户代理标头,默认情况下python会为您添加,Google可以检测到它并且可能被禁止。

如果可以提供帮助,请尝试以下操作。

import urllib
yourUrl = "post it here"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}  
req = urllib.request.Request(yourUrl, headers = headers)
page = urllib.request.urlopen(req)