gpt4 book ai didi

用于检查 url 是否存在的 Python 脚本因 HTTPConnectionPool 错误而失败

转载 作者:行者123 更新时间:2023-12-04 10:45:15 25 4
gpt4 key购买 nike

我编写了以下 python 脚本来检查前缀和后缀的可能排列是否作为网站 url 存在:

import requests

prefix = ['test', 'trial','demo','check']
suffix = ['site','web','page']

results = []
domains = []
site_available = []
site_occupied = []

for elem in prefix:
for i in suffix:
results.append(elem+i)

for i in results:
domains.append('http://www.{}.com'.format(i))


for site in domains:
request = requests.get(site)
if request.status_code == 200:
site_occupied.append(site)
else:
site_available.append(site)

不幸的是,我一直看到以下错误:
ConnectionError: HTTPConnectionPool(host='www.testpage.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x11326ccd0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))

我该如何解决这个错误?
非常感谢!

最佳答案

您只需要一个 try-except要处理的块 ConnectionError .我会将它添加到循环中:

for site in domains:
try:
request = requests.get(site)
except requests.ConnectionError as ex:
<exception handling code here>
continue # <--- move to next loop iteration without trying to process a failed request

这可能不是进行异常处理的最健壮的方式,但它应该适用于您正在做的事情。

关于用于检查 url 是否存在的 Python 脚本因 HTTPConnectionPool 错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59736743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com