Python requests 库出现 'Connection aborted' 错误:远程端关闭连接
这个错误可能是因为你的请求被服务器端拒绝或连接超时,导致远程端关闭连接。请确保你的网络连接正常,并且确认你可以访问该网站。另外,建议你在请求中添加适当的 Headers 信息,以模拟正常的浏览器请求。
你可以尝试以下代码:
import requests
url = 'https://www.kuaidaili.com/free/inha/{}/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
html = response.content.decode('utf-8')
# 处理HTML内容
except requests.exceptions.RequestException as e:
print('请求发生异常:', e)
请确保替换'User-Agent'的值为你正在使用的浏览器的信息。如果问题仍然存在,请尝试使用其他代理或联系网站管理员以获取更多帮助。
原文地址: https://www.cveoy.top/t/topic/SGV 著作权归作者所有。请勿转载和采集!