我尝试了python请求库文档中提供的示例。
使用async.map(rs),我得到响应代码,但我想获取请求的每个页面的内容。例如,这不起作用:
async.map(rs)
out = async.map(rs) print out[0].content
以下答案 不适 用于请求 v0.13.0+。编写此问题后,异步功能已移至grequests 。但是,你可以requests用grequests下面的替换它应该可以工作。
requests
grequests
我留下这个答案是为了反映关于使用 requests < v0.13.0 的原始问题。
要async.map 异步 执行多项任务,您必须:
async.map
例子:
from requests import async # If using requests > v0.13.0, use # from grequests import async urls = [ 'http://python-requests.org', 'http://httpbin.org', 'http://python-guide.org', 'http://kennethreitz.com' ] # A simple task to do to each response object def do_something(response): print response.url # A list to hold our things to do via async async_list = [] for u in urls: # The "hooks = {..." part is where you define what you want to do # # Note the lack of parentheses following do_something, this is # because the response will be used as the first argument automatically action_item = async.get(u, hooks = {'response' : do_something}) # Add the task to our list of things to do via async async_list.append(action_item) # Do our list of things to do via async async.map(async_list)