如何以最快的方式发送 1000 个请求?
我知道您可以使用 grequests
发送多个请求:
urls = [
'sample.url/1',
'sample.url/2',
...
]
request = (grequests.get(u) for u in urls)
print grequests.map(request)
但返回不是内容。
我需要的是获取 json 数据,例如这样的事情:
request = (grequests.get(u) for u in urls)
content = grequests.json(request)
最佳答案
返回的项目不是内容,但它们确实包含内容。您可以像这样获取所有内容:
result = grequests.map(request)
content = '\n'.join(r.content for r in result) # raw content
text = '\n'.join(r.text for r in result) # decoded content
您可以像这样解析 json:
result = grequests.map(request)
json = [r.json() for r in result]
示例程序:
import grequests
import pprint
urls = [
'http://httpbin.org/user-agent',
'http://httpbin.org/headers',
'http://httpbin.org/ip',
]
requests = (grequests.get(u) for u in urls)
responses = grequests.map(requests)
json = [response.json() for response in responses]
pprint.pprint(json)
text = '\n'.join(response.text for response in responses)
print(text)
关于Python:如何发送多个 HTTP 请求并接收响应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32829798/