问题
What is the proper way to Google something in Python 3? I have tried requests
and urllib
for a Google page. When I simply res = requests.get("https://www.google.com/#q=" + query)
that doesn't come back with the same HTML as when I inspect the Google page in Safari. The same happens with urllib
. A similar thing happens when I use Bing. I am familiar with AJAX
. However, it seems that that is now depreciated.
回答1:
In python, if you do not specify the user agent header in http requests manually, python will add for you by default which can be detected by Google and may be forbidden by it.
Try the following if it can help.
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)
来源:https://stackoverflow.com/questions/38484268/how-to-google-in-python-using-urllib-or-requests