python - 请求整个请求的超时?

时间:2022-10-14 21:24:58

I am crawling many sites for data, but some links are freezing my script permanently. This shouldn't happen, since I used a timeout like this :

我正在抓取许多网站的数据,但有些链接会永久冻结我的脚本。这不应该发生,因为我使用了这样的超时:

page = requests.get(url,timeout=4)

I want a timeout for the whole request. So when the request take 4 seconds it will stop trying. I searched requests documentation, and I found this code for read and connect timeout:

我希望整个请求超时。因此,当请求需要4秒时,它将停止尝试。我搜索了请求文档,我发现这个代码用于读取和连接超时:

r = requests.get(url, timeout=(3.05, 27))

However, I get a type error when I try and use it:

但是,当我尝试使用它时,我收到类型错误:

Timeout value connect was (3.05, 27), but it must be an int or float.

How can I get the timeout I want?

我怎样才能得到我想要的超时?

1 个解决方案

#1


7  

Based on a related issue here with docker, it is a bug in python-requests that has been fixed in python-requests version 2.4.3-4. Upgrade to the latest version and you should be fine.

基于与docker相关的问题,这是python-requests版本2.4.3-4中修复的python请求中的一个错误。升级到最新版本,你应该没问题。

If you have pip use pip install -U requests

如果你有pip使用pip install -U请求

#1


7  

Based on a related issue here with docker, it is a bug in python-requests that has been fixed in python-requests version 2.4.3-4. Upgrade to the latest version and you should be fine.

基于与docker相关的问题,这是python-requests版本2.4.3-4中修复的python请求中的一个错误。升级到最新版本,你应该没问题。

If you have pip use pip install -U requests

如果你有pip使用pip install -U请求