Home >Backend Development >Python Tutorial >How to Retrieve Page Content from Asynchronous Requests in Python\'s Requests Library?
Challenge:
While the Requests library's documentation provides an example for performing asynchronous requests, it lacks guidance on retrieving page content. The following code fails to access the content:
out = async.map(rs) print out[0].content
Solution:
To perform multiple asynchronous tasks using async.map, follow these steps:
Example:
Consider a list of URLs and a simple task of printing the response URL:
import async urls = [ 'http://python-requests.org', 'http://httpbin.org', 'http://python-guide.org', 'http://kennethreitz.com' ] def do_something(response): print(response.url) async_list = [] for u in urls: action_item = async.get(u, hooks={'response': do_something}) async_list.append(action_item) async.map(async_list)
The above is the detailed content of How to Retrieve Page Content from Asynchronous Requests in Python\'s Requests Library?. For more information, please follow other related articles on the PHP Chinese website!