-
Notifications
You must be signed in to change notification settings - Fork 10.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
response.json()? #2444
Comments
@eliasdorneles proposed it here: #1729 (comment). I think that's fine to add it. Not sure if we need JsonResponse - maybe it'd be nice to have, to be able to check if json is available. But maybe it doesn't worths it, I'm not sure. |
The question is whether should response.json raise AttributeError if response is not JSON; if it should (like .text, .css and .xpath) then we need JsonResponse. If it shouldn't then we don't need JsonResponse. |
python-requests provide it by default and just fail with ValueError if body is not json. Of course their implementation is not necessarily ideal, maybe we can do better. We could check for response headers and check if they have # some spider callback
if isinstance(response, scrapy.response.JSONResponse):
data = response.json()
else:
raise ValueError('expected JSON') instead of: data = response.json() |
@kmike oops, only |
About I've been thinking that the |
If we do include the |
Regarding jmespath - see also scrapy/parsel#27. As for .json(), is it just a shortcut for Supporting .json() it in selectors sounds good, but I'd wait for real use cases / examples for that. |
mark |
I was thinking about However, I don't think it should be only a wrapper for
But, it would be better to load the JSON as a python object and then write
|
Would love to have this feature. |
I have added a PR which is being reviewed by @elacuesta at the moment, please let me know if it goes in the right direction. |
python-requests have response.json() that decodes json body and returns appropriate Python objects. Does it make sense to have something like this in Scrapy?
The text was updated successfully, but these errors were encountered: