我尝试运行此代码,但始终收到错误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))
有没有人可以帮我这个代码,因为我在这里找不到解决方案?
答案 0 :(得分:1)
更改此行
url = urllib.request.urlopen('http://google.com/search?q=test')
到
url = 'http://google.com/search?q=test'
答案 1 :(得分:0)
看起来你打开了两次网址。这里有两条建议:
为什么不直接使用requests
模块?首先,安装它:
pip install requests
使用with..as
上下文管理器来处理文件I / O
import requests
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' }
resp_data = requests.get('http://google.com/search?q=test', headers=headers).text
with open('newFile.txt', 'w') as savefile:
savefile.write(resp_data)