简体   繁体   English

如何将 grequests 响应与请求映射?

[英]How to map grequests response with request?

I wrote a python code to send async requests using grequests, how to check which request's response is this ?我写了一个 python 代码来使用 grequests 发送异步请求,如何检查这是哪个请求的响应? are responses are in same order as they are sent ?响应的顺序是否与发送的顺序相同?

Here is my code :这是我的代码:

import grequests
import time

start_time = time.time()

q_values = [1,2,3,4,5,6,7,8,9,10]

headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Upgrade-Insecure-Requests": "1"}
rs = (grequests.get("https://www.google.com?query="+str(u) , headers=headers) for u in q_values)


rs = grequests.map(rs,size=10)

for r in rs:
 print r

I need output like this :我需要这样的输出:

q_value 1 response code is <Response [200]>
q_value 2 response code is <Response [200]>
q_value 3 response code is <Response [200]>
q_value 4 response code is <Response [200]>

Ok i did it with grequests hooks, here is my answer :好的,我是用 grequests 钩子做的,这是我的答案:

import grequests
import time

start_time = time.time()

q_values = [1,2,3,4,5,6,7,8,9,10]

def do_something(response, *args, **kwargs):
    url = response.url
    print (url+ " : " +str(response))


headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Upgrade-Insecure-Requests": "1"}
rs = (grequests.get("https://www.google.com?query="+str(u) , headers=headers, hooks = {'response' : do_something}) for u in q_values)


rs = grequests.map(rs,size=10)

any expert can confirm is it correct ?任何专家都可以确认它是正确的吗?

In the response object, it also store the request object according the response在响应对象中,它还根据响应存储请求对象

rs = grequests.map(rs,size=10)

for r in rs:
   print r.request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM