According the Requests documentation, event hooks can be added to .get() function.
requests.get('http://httpbin.org', hooks=dict(response=print_url))
def print_url(r, *args, **kwargs):
print(r.url)
This is fine but how to set *args with custom parameters, for example, I want to pass some custom values to print_url(), how to set those in *args ? Something like this fails :
args = ("search_item", search_item)
rs = (grequests.get(u, hooks={'response': [parse_books],'args': [args]}) for u in urls)
You cannot specify extra arguments to pass to a response hook. If you want extra information to be specified you should make a function that you call which then returns a function to be passed as a hook, e.g.,
def hook_factory(*factory_args, **factory_kwargs):
def response_hook(response, *request_args, **request_kwargs):
# use factory_kwargs
# etc.
return None # or the modified response
return response_hook
grequests.get(u, hooks={'response': [hook_factory(search_item=search_item)]})
the response_hook
function has to return a response-object. the simplest workaround would be
to modify the respose object you get from the hook_factory calling the hook.
def response_hook(response, *request_args, **request_kwargs):
# use factory_kwargs
# etc.
response.meta1='meta1' #add data
response.meta2='meta2'
#etc.
return response# or the modified response
return response_hook
hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With