爬虫代理卡住:关于requests里的timeout()

https://blog.csdn.net/qq_38251616/article/details/81813793

在爬虫代理这一块我们经常会遇到请求超时的问题,代码就卡在哪里,不报错也没有requests请求的响应。

通常的处理是在requests.get()语句中加入timeout限制请求时间

req = requests.get(url, headers=headers, proxies=proxies, timeout=5)

如果发现设置timeout=5后长时间不响应问题依然存在,可以将timeout里的参数细化
作出如下修改后,问题就消失了

req = requests.get(url, headers=headers, proxies=proxies, timeout=(3,7))
--------------------- 

timeout=(3,7) = (connect timeout,read timeout)

timeout是用作设置响应时间的,响应时间分为连接时间和读取时间,timeout(3,7)表示的连接时间是3,响应时间是7,就是酱紫的

猜你喜欢

转载自blog.csdn.net/NRlovestudy/article/details/88311814