import re
>>> import urllib.request
>>> url="https://www.google.com/search?q=googlestock"
>>> print(url)
https://www.google.com/search?q=googlestock
>>> data=urllib.request.urlopen(url).read()
我收到一个错误,但是手动打开时网址工作正常。 错误是
File "<pyshell#4>", line 1, in <module>
data=urllib.request.urlopen(url).read()
File "C:UsersSHARMAppDataLocalProgramsPythonPython37-32liburllibrequest.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:UsersSHARMAppDataLocalProgramsPythonPython37-32liburllibrequest.py", line 531, in open
response = meth(req, response)
File "C:UsersSHARMAppDataLocalProgramsPythonPython37-32liburllibrequest.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:UsersSHARMAppDataLocalProgramsPythonPython37-32liburllibrequest.py", line 569, in error
return self._call_chain(*args)
File "C:UsersSHARMAppDataLocalProgramsPythonPython37-32liburllibrequest.py", line 503, in _call_chain
result = func(*args)
File "C:UsersSHARMAppDataLocalProgramsPythonPython37-32liburllibrequest.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
如果你想从谷歌进行网页抓取,你可以使用"谷歌"库。在您的命令提示符下,pip install google(字面意思是"pip install google"(。然后,尝试这样的事情:
from googlesearch import search
for s in search("googlestock"):
print(s)
这将打印来自谷歌搜索"googlestock"的所有结果。在这里了解有关此库的更多信息:https://pypi.org/project/google/
我希望它有所帮助,BR