我尝试了文档中提供的示例请求库对于蟒蛇。
With async.map(rs)
,我得到了响应代码,但我想获取所请求的每个页面的内容。例如,这不起作用:
out = async.map(rs)
print out[0].content
Note
下面的答案是not适用于请求 v0.13.0+。异步功能已移至请求这个问题写完之后。但是,您可以只替换requests
with grequests
下面它应该可以工作。
我保留这个答案是为了反映关于使用 requests
执行多项任务async.map
异步地你必须:
- 为您想要对每个对象执行的操作定义一个函数(您的任务)
- 将该函数添加为请求中的事件挂钩
- Call
async.map
在所有请求/操作的列表上
Example:
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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)