why do I can't extract data from response using grequests?

401 Views Asked by At
import grequests
import time
start_time = time.time()

sites = ['https://facebook.com' for x in range(5)]
data = {'a': 'b'}


responses = [grequests.get(u, data=data) for u in sites]

for response in responses:
    print(response)
    print(f'{response.text}')

print("--- %s seconds ---" % (time.time() - start_time))

When I try to get data by the .text method, I have an error:

AttributeError: 'AsyncRequest' object has no attribute 'text'

Even If I try to get status_code I have the same error with missing the attribute

The command to fix that I've searched pip3 install --upgrade gevent==1.1rc3 returning me an error: fatal error: Python.h: No such file or directory into 5 | #include "Python.h"

1

There are 1 best solutions below

0
furas On

When module use async then it may run code in separated thread and it doesn't wait for result but it gives you object which you can later use to check if there is already result. In some modules you may use await to wait for result (but this works only inside async function and it needs to run async loop) but some modules have special functions to wait for results.

And grequests has also special function for this - which you should see in official examples

responses = grequests.map(responses)

If you use it then you get list of objects <Response>. Without map() you get only list of objects <grequests.AsyncRequest>


Minimal working example:

import grequests
import time

start_time = time.time()


sites = ['https://facebook.com' for x in range(5)]
data = {'a': 'b'}

responses = [grequests.get(u, data=data) for u in sites]

# wait for all results
responses = grequests.map(responses)

for response in responses:
    print('response:', response)
    print('status_code:', response.status_code)
    print('text:', response.text)

print("--- %s seconds ---" % (time.time() - start_time))