“‘连接已中止’、‘远程断开连接’”或无法使用urllib3和Django获取任何信息

2024-05-18 08:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用BeautifulSoup和Django开发一个web抓取应用程序,我遇到了一些“conexion问题”(我想)

应用程序必须检查任何网站是否满足所有SEO要求,为此,我必须提出不同的“请求”。。。首先获取“汤”,然后检查robots.txt和sitemap.xml是否存在。。。所以我猜一些网站正是因为这个原因阻止了我的应用程序,我一直收到“连接中止”,“远程断开连接”错误,或者在其他情况下,我没有收到错误,但“汤”是空的。。。有办法解决这个问题吗?我试过使用time.sleep(),但似乎不起作用

这是我代码的一部分:

 http = PoolManager()
 r = http.request('GET', "https://" + url, headers={'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36", 'Accept-Encoding': 'br'})
    
 soup = BeautifulSoup(r.data, 'lxml')

我在哪里检查机器人和站点地图是否存在:

robots_url = url + "/robots.txt"
robot = requests.get(robots_url, headers)

if robot.ok:
    robot = True
else:
    robot = False

sleep(5)

sitemap_url = url + '/sitemap.xml'
sitemap = requests.get(sitemap_url, headers=headers)
if sitemap.ok:
    sitemap = True
else:
    sitemap = False

在大多数网站中,代码工作正常,但我认为有些页面具有更高的安全级别,以该错误结束连接:

During handling of the above exception (('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))), another exception occurred:

/app/.heroku/python/lib/python3.9/site-packages/django/core/handlers/exception.py,第47行,在内部

提前非常感谢您的时间和建议


Tags: 代码txt应用程序httpurl网站错误exception