Please how can I print out value of keys Id, isbn and others | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please how can I print out value of keys Id, isbn and others

res= requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "xxx", "isbns": "9781632168146"}) # print(res.json()) outputs: {'books': [{ 'id': 47173379, 'isbn': '0441172717', 'isbn13': '9780441172719', 'ratings_count': 33, 'reviews_count': 98, 'text_reviews_count': 3, 'work_ratings_count': 632097, 'work_reviews_count': 1050081, 'work_text_reviews_count': 17190, 'average_rating': '4.22' }]} how do print values for id, isbn and others? Thanks

27th Aug 2019, 10:07 AM
Kayode Adetayo
Kayode Adetayo - avatar
4 Answers
+ 6
hi, it was not possible to post my message here, so i put it to a python file: https://code.sololearn.com/cUKOmgBECzU4/?ref=app
27th Aug 2019, 2:16 PM
Lothar
Lothar - avatar
+ 2
It seems the requests.json() function already converts the output to a python dictionary. Based on your print output, this dictionary has a single key (books) and the value is a list that has one element, which in turn is another dictionary. So in summary you may not need the json module at all. As long as you query only one book, you should be able to access the details by indexing into the nested data structures. Something like: JS = res.json() print(JS['books'][0]['isbn']) Check also here: https://realpython.com/python-requests/
27th Aug 2019, 2:44 PM
Tibor Santa
Tibor Santa - avatar
+ 2
thank you everyone. Tibor Santa it worked. I’ll read up the url link you provided.
27th Aug 2019, 3:14 PM
Kayode Adetayo
Kayode Adetayo - avatar
+ 1
import requests, json res= requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "xxx", "isbns": "9781632168146"}) JS = res.json() bs = json.loads(JS) print(bs.get['id']) File "C:\Python\lib\json\__init__.py", line 341, in loads raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not dict [Finished in 2.9s with exit code 1] is the error message I got NOTE: print(JS) Prints out:- ---------------------------------------------- {'books': [{'id': 29207858, 'isbn': '1632168146', 'isbn13': '9781632168146', 'ratings_count': 0, 'reviews_count': 2, 'text_reviews_count': 0, 'work_ratings_count': 28, 'work_reviews_count': 123, 'work_text_reviews_count': 10, 'average_rating': '4.07'}]} [Finished in 3.2s] what I'll like to do is render/display isbn and work_ratings_count values on bookdetail.html
27th Aug 2019, 11:08 AM
Kayode Adetayo
Kayode Adetayo - avatar