python - 如何使用请求模块跳过连接超时 url
问题描述
嗨,我如何使用请求模块来处理一堆 URL,如果列表中的 URL 需要更多时间来加载或连接超时,我如何跳过该特定 URL 并跳到下一个
def req():
with open('demofile.txt','r') as http:
for url in http.readlines():
req = url.strip()
print(req)
page=requests.get("http://"+req,verify=False)
if page.status_code == 400:
break
else:
continue
time.sleep(1)
解决方案
如果超时,您可以引发异常并继续finally
阻止下一个请求,
import requests
import logging
timeout = 0.00001
try:
response = requests.get(url="https://google.com", timeout=timeout)
except requests.exceptions.ConnectTimeout as e:
logging.error("Time out!")
finally:
# continue request here
print("hello")
# output,
ERROR:root:Time out!
hello
推荐阅读
- linux - 如何区分两个正在运行的同名Linux脚本?
- powershell - 从 Powershell 调用 Perl 脚本并将脚本输出存储在变量中
- python - 为什么 tensorflow 的 tf.random.uniform 在第一次运行时运行缓慢?
- visual-studio-code - 如何在扩展构建时确定 vscode 的当前和未来电子版本
- python - 添加一定数量的图像时,按钮网格垂直收缩
- vue.js - Vuetifyjs 2.0 主题不会随观察者而改变
- ios - 如何将 .arobject 文件转换为 .usdz 文件?
- javascript - vue js数据无法循环
- c++ - 为什么我收到转换函数错误?
- listview - Yii2 listview分页位置