网页爬虫 - requests中timeout问题

查看:167
本文介绍了网页爬虫 - requests中timeout问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

requests.get(url,headers=headers,timeout=130)
我调用一个接口,设置了timeout时间为130,然后调用时,感觉他们的接口一直停在那里,没有响应,没有输出,也没有报错。过了130秒之后,也没有timeout错误。直到4分钟之后,接口正常输出结果。
请问有人遇到过这个情况么?
为什么timeout没作用?

解决方案

timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.

官网 上如是说

简单来说就是 timeout 并不是整个下载响应的时间限制,而是如果服务器在 timeout 秒内没有应答,将会引发一个异常。timeout能设置到130我猜大概是你网速不好吧。过程我觉得是130s内收到应答,后来的时间用来下载数据.

这篇关于网页爬虫 - requests中timeout问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆