【发布时间】:2019-01-30 03:31:29
【问题描述】:
我有一个大约 250K 的 URL 列表,用于我需要检索的 API。
我使用grequests 制作了一个类,它完全按照我想要的方式工作,但我认为它工作得太快了,因为在运行了整个 URL 列表后我得到了错误:
Problem: url: HTTPSConnectionPool(host='url', port=123): Max retries exceeded with url: url (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x38f466c18>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
到目前为止的代码:
import grequests
lst = ['url','url2',url3']
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=5)
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()
如何降低代码速度以防止出现“最大重试次数错误”?或者更好的是如何将我拥有的列表分块并以块的形式传递 URL?
在 mac 上使用 python3.6。
编辑:
问题不重复,必须将许多 URL 传递到同一个端点。
【问题讨论】:
-
@klanmiko 不是重复的,需要传入一个 URL 列表而不仅仅是一个
标签: python python-3.x api-design grequests