Python Forum
python requests library .JSON() error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: python requests library .JSON() error (/thread-39010.html)



python requests library .JSON() error - mHosseinDS86 - Dec-19-2022

Can someone explain this? I'm trying to use python requests library, its attributes and methods. while using .JSON()
method I get this error. How should I fix it and why this error? thanks in advance.


import requests
url="https://www.google.com/"
response=requests.get(url).text
print(response.json())
=======================


---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)

----> 7 print(response.json())

AttributeError: 'str' object has no attribute 'json'


RE: python requests library .JSON() error - deanhystad - Dec-19-2022

If you want json, ask for json. You are asking for text.
import requests
url="https://www.google.com/"
response=requests.get(url)  # Leave as a reply
print(response.text)  # If you want to see text
print(response.json())  # If you want to see json



RE: python requests library .JSON() error - mHosseinDS86 - Dec-19-2022

Thanks for the reply. I'm still getting this error.

JSONDecodeError: Expecting value: line 1 column 1 (char 0)


RE: python requests library .JSON() error - deanhystad - Dec-19-2022

My guess is response=requests.get(url) failed and response is blank nor None. What happens if your print(response)?


RE: python requests library .JSON() error - mHosseinDS86 - Dec-19-2022

I get this response:
<Response [200]>


RE: python requests library .JSON() error - snippsat - Dec-19-2022

You can not get json directly from the google address,have to use there Google Search JSON API
As mention you most look at what you get back.
import requests

url ="https://www.google.com/"
response = requests.get(url)
print(response.content)
Which would be a little HTML and lot of JavaScripts.


RE: python requests library .JSON() error - deanhystad - Dec-19-2022

what is response.text? I don't think www.google.com will download google as a json file