在循环中抓取多个网站时,我注意到速度之间存在相当大的差异,
sleep(10)
response = requests.get(url)
和,
response = requests.get(url, timeout=10)
也就是说,timeout
要快得多。
此外,对于这两种设置,我希望在请求下一页之前每页至少10秒的抓取持续时间,但事实并非如此。
我现在使用多处理,但我想要记住上述保留以及非多处理。
答案 0 :(得分:1)
time.sleep
会阻止您的脚本运行一定的秒数,而timeout
是等待检索该网址的最长时间。如果在timeout
时间到来之前检索到数据,则会跳过剩余时间。因此,使用timeout
可能需要不到10秒的时间。
time.sleep
不同,它会完全暂停您的脚本,直到它完成睡眠状态,然后它会再运行您的请求几秒钟。所以time.sleep
每次都需要10秒以上。
它们有非常不同的用途,但对于你的情况,你应该制作一个计时器,如果它在10秒之前完成,让程序等待。
答案 1 :(得分:0)
response = requests.get(url, timeout=10)
# timeout specifies the maximum time program will wait for request to complete before throwing exception. It is not necessary that program will pause for 10 seconds. If response is returned early the program won't wait anymore.
详细了解requests
超时here。
time.sleep
导致您的主线程处于休眠状态,因此您的程序将始终在向网址发出请求之前等待10秒。