作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个Python代码来使用grequests发送异步请求,如何检查这是哪个请求的响应?回复的顺序与发送的顺序相同吗?
这是我的代码:
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
我需要这样的输出:
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]>
最佳答案
好吧,我用 grequests hooks 做到了,这是我的答案:
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)
任何专家都可以确认它是否正确?
关于python - 如何将请求响应与请求映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54359840/
我是一名优秀的程序员,十分优秀!