Hakki
(Hakki Tomanbay)
August 21, 2020, 9:47pm
1
Hi,
I am trying to get the content to print as a python list. But it says json.loads(page) needs to be a string? When I use pythonlist = page.json() it works. Why is that? Should I never use json.loads() and always use .json()?
page = requests.get(“https://api.datamuse.com/words?ml=ringing+in+the+ears ”)
pythonlist = json.loads(page)
print(pythonlist)
pylang
(py)
August 22, 2020, 5:53pm
2
What is the type of page
? E.g. type(page)
.
1 Like
maciek
(Maciej Olko)
August 22, 2020, 11:13pm
3
json() method of response, that is shipped with Requests library, does loading for you. If you don’t want to use this shortcut, you can do:
pythonlist = json.loads(page.content)
page is a response object that has other attributes than sole content.
1 Like
Hakki
(Hakki Tomanbay)
August 23, 2020, 12:55am
4
print(type(page))
output:
<class ‘requests.models.Response’>
Hakki
(Hakki Tomanbay)
August 23, 2020, 12:57am
5
Got you. That makes sense. Also I believe I can do:
pythonlist = json.loads(page.text)
Instead of page.content, page.text also works. Is there a reason I should use the one over the other?
maciek
(Maciej Olko)
August 26, 2020, 10:49pm
6