[英]HTTP Error 403: Forbidden when using urllib.request
I tried to run this code, but all the time I receive the error HTTP Error 403: Forbidden
. 我尝试运行此代码,但始终收到错误
HTTP Error 403: Forbidden
。
import urllib.request
try:
url = urllib.request.urlopen('http://google.com/search?q=test')
headers = {}
headers['User-Agent'] ='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
req = urllib.request.Request(url, headers=headers)
resp = urllib.request.urlopen(req)
resp_Data = resp.read()
savefile = open('newFile.txt', 'w')
savefile.write(str(resp_Data))
savefile.close()
except Exception as e:
print(str(e))
Could anyone help me in this code, because I couldn't find a solution here? 有人可以在这段代码中为我提供帮助,因为我在这里找不到解决方案吗?
Change this line 更改此行
url = urllib.request.urlopen('http://google.com/search?q=test')
to 至
url = 'http://google.com/search?q=test'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.