2

我正在使用 grequests 库传递 ~250000 个 url 以从 api 获取数据。

API 限制为每秒 100 次调用。

如何限制 grequests 每秒只传递 100 个 url?我将大小参数从 5 增加到 100。不确定这是做什么的,但仍然运行到错误“超出最大重试次数”。

到目前为止,这是我的代码:

import grequests

lst = ['url.com','url2.com']

class Test:
    def __init__(self):
        self.urls = lst

    def exception(self, request, exception):
        print ("Problem: {}: {}".format(request.url, exception))

    def async(self):
        return grequests.map((grequests.get(u) for u in self.urls), exception_handler=self.exception, size=100)

    def collate_responses(self, results):
        return [x.text for x in results]

test = Test()
#here we collect the results returned by the async function
results = test.async()

response_text = test.collate_responses(results)
4

1 回答 1

1

Grequests 似乎发出了 100 个请求,然后没有任何等待就发出了另外 100 个请求,依此类推。这些请求之间没有定义时间。这是一个用解决方案描述的类似问题: Limiting/throttling the rate of HTTP requests in GRequests

于 2019-07-14T08:57:57.143 回答