Python Forum

Full Version: python requests library .JSON() error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'
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
Thanks for the reply. I'm still getting this error.

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
My guess is response=requests.get(url) failed and response is blank nor None. What happens if your print(response)?
I get this response:
<Response [200]>
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.
what is response.text? I don't think www.google.com will download google as a json file