请求网站时报错:http.client.RemoteDisconnected: Remote end closed connection without response
原因:服务器限制了部分User-Agent的访问
解决方案: 添加User-Agent
原代码:
data = urllib.request.urlopen(url).read().decode("utf-8", "ignore")
修改后:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'}
data = urllib.request.Request(url, headers=headers)
data_1 = urllib.request.urlopen(data).read().decode("utf-8", "ignore")