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()
我收到一个错误,但是手动打开该URL可以正常工作。错误是
File "<pyshell#4>", line 1, in <module>
data=urllib.request.urlopen(url).read()
File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
答案 0 :(得分:0)
如果您想从Google进行网页抓取,则可以使用“ google”库。 在命令提示符下,pip install google(字面意思是“ pip install google”)。 然后,尝试类似的事情:
from googlesearch import search
for s in search("googlestock"):
print(s)
这将打印来自Google搜索“ googlestock”的所有结果。这里以了解有关此库的更多信息:https://pypi.org/project/google/
我希望它会有所帮助, BR