【问题标题】:How to limit http request number per second on Python?如何在 Python 上限制每秒的 http 请求数?
【发布时间】:2016-03-02 10:51:33
【问题描述】:

如何编写代码实现用python限制每秒http请求数的功能?

比如我给python脚本指定了100个请求号,那么脚本每秒只会发送100个url到服务器。

我搜索到了这个链接:Limiting number of HTTP requests per second on Python。但是,我没有从这个页面得到它。

【问题讨论】:

  • PS:我使用的是 Python 2.7,所以 Python 3+ 的库不适合我。谢谢
  • 你能发布你的代码吗?你使用线程等,...?
  • 我现在没有代码。我希望得到解决方案然后编写代码。当然,我可以使用线程、队列等……
  • 为什么不在请求之间简单地做一个 time.sleep() 呢?

标签: python python-2.7 web-crawler urllib2


【解决方案1】:

如果您同时只需要一个请求,您可以使用 time.sleep(1) 来完成。

import requests
import time

r = requests.get("http://www.google.com")
print(r)
time.sleep(1)
r = requests.get("http://www.stackoverflow.com")
print(r)
time.sleep(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    相关资源
    最近更新 更多