我正在收集网站列表上的统计信息,并且为了简化起见,我使用了对此的请求。这是我的代码:data=[]websites=['http://google.com', 'http://bbc.co.uk']for w in websites: r= requests.get(w, verify=False) data.append( (r.url, len(r.content), r.elapsed.total_seconds(), str([(l.status_code, l.url) for l in r.history]), str(r.headers.items()), str(r.cookies.items())) )现在,我想requests.get在10秒后超时,以免循环陷入困境。这个问题以前也很有趣,但是没有一个答案是正确的。我会为此悬赏,以得到一个不错的答案。我听说也许不使用请求是一个好主意,但是我应该如何获得请求所提供的好处。(元组中的)
3 回答

LEATH
TA贡献1936条经验 获得超6个赞
设置超时参数:
r = requests.get(w, verify=False, timeout=10)
只要您未stream=True对该请求进行设置,requests.get()如果连接花费的时间超过十秒,或者服务器发送的数据时间不超过十秒,这将导致呼叫超时。
添加回答
举报
0/150
提交
取消