Remove the quotes from around "isbn" as such:
bad:
>>> requests.get('https://www.goodreads.com/book/review_counts.json', params={"key": "[MY_API_KEY]", "isbns": "isbn"}).json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
good, sending the value of the variable instead of the variable:
>>> isbn="0441172717"
>>> requests.get('https://www.goodreads.com/book/review_counts.json', params={"key": "[MY_API_KEY]", "isbns": isbn}).json()
{'books': [{'id': 47173379, 'isbn': '0441172717', 'isbn13': '9780441172719', 'ratings_count': 3, 'reviews_count': 8, 'text_reviews_count': 0, 'work_ratings_count': 625868, 'work_reviews_count': 1034615, 'work_text_reviews_count': 17000, 'average_rating': '4.22'}]}
better:
...
r = requests.get("https://www.goodreads.com/book/review_counts.json", params={"key": "[MY_API_KEY]", "isbns": isbn})
if r.status_code != 200:
raise ValueError
...
best: check isbn for validity first and put your db.execute() first. Why bother goodreads.com if your DB doesn't have a result? Unless, you're counting on goodreads.com for making sure user input is correct and is protecting you from injection...?
Next, don't use your your real API key in a public question ;)
And finally, requests.get(...).json() wont work if there's an error:
>>> requests.get('https://www.goodreads.com/book/review_counts.json')
<Response [422]>
From goodreads.com/api: "You can mix ISBN10s and ISBN13s, but you'll receive a 422 error if you don't specify any, and you'll receive a 404 if none are found."
You were sending "isbns=isbn" i.e. no real valid isbn.
Hope this helps!
Answering your question from your further comment:
Reformatting the response above into "pretty JSON" (google JSON viewer for an online way of copy+pasting JSON string to beautify) we see:
{
'books':[
{
'id':47173379,
'isbn':'0441172717',
'isbn13':'9780441172719',
'ratings_count':3,
'reviews_count':8,
'text_reviews_count':0,
'work_ratings_count':625902,
'work_reviews_count':1034690,
'work_text_reviews_count':17002,
'average_rating':'4.22'
}
]
}
You might now be able to see that the response is a Dict(Response)->Array(books)->Dicts so..
>>> r = requests.get('https://www.goodreads.com/book/review_counts.json', params={"key": "L3FHyOR3IhCo3kctcUz3zg", "isbns": isbn}).json()
>>> print(r)
{'books': [{'id': 47173379, 'isbn': '0441172717', 'isbn13': '9780441172719', 'ratings_count': 3, 'reviews_count': 8, 'text_reviews_count': 0, 'work_ratings_count': 625905, 'work_reviews_count': 1034694, 'work_text_reviews_count': 17002, 'average_rating': '4.22'}]}
>>> print(r['books'])
[{'id': 47173379, 'isbn': '0441172717', 'isbn13': '9780441172719', 'ratings_count': 3, 'reviews_count': 8, 'text_reviews_count': 0, 'work_ratings_count': 625905, 'work_reviews_count': 1034694, 'work_text_reviews_count': 17002, 'average_rating': '4.22'}]
>>> print(r['books'][0])
{'id': 47173379, 'isbn': '0441172717', 'isbn13': '9780441172719', 'ratings_count': 3, 'reviews_count': 8, 'text_reviews_count': 0, 'work_ratings_count': 625905, 'work_reviews_count': 1034694, 'work_text_reviews_count': 17002, 'average_rating': '4.22'}
>>> print(r['books'][0]['reviews_count'])
8
So, your answer to the attribute error is to use: "r['books'][0]['reviews_count']" not "r.reviews_count" as well as "r['books'][0]['?????']" format for any other attributes. You could also handle multiple ISBNs by the format "########,########,########,..." and handling multiple responses in your code according to the API.
resso you see its format? It could be a numeric issue withaverage_ratingfor being decimal. Have you tried usingsqlalchemyobjects instead of directSQL? (also be careful with SQL injection the way you coded it)params={... "isbns": "isbn"}-- it looks like you are passing the"isbn"string here instead of the ISBN number.