Python Forum
python requests library .JSON() error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python requests library .JSON() error
#1
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'
Larz60+ write Dec-19-2022, 07:25 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post.
Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed this time, please use BBCode on future posts.
Reply
#2
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
Reply
#3
Thanks for the reply. I'm still getting this error.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 685 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  Error in blend_modes library talgreen 6 995 Jul-01-2023, 05:32 PM
Last Post: talgreen
  json decoding error deneme2 10 3,657 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,368 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Python Split json into separate json based on node value CzarR 1 5,606 Jul-08-2022, 07:55 PM
Last Post: Larz60+
Question I am struggling with basic json library + sql alchemy w/ mariadb-connector json ->sql BrandonKastning 0 1,516 Mar-04-2022, 09:26 PM
Last Post: BrandonKastning
  Install any library via pip get an error cannot import name 'SCHEME_KEYS' from 'pip. Anldra12 2 10,640 Jan-04-2022, 01:05 PM
Last Post: Anldra12
  Python requests oauth2 access token herobpv 6 3,923 Sep-27-2021, 06:54 PM
Last Post: herobpv
  Error about parser library and tree builder Led_Zeppelin 8 11,521 Jun-11-2021, 08:53 PM
Last Post: snippsat
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,946 Jan-11-2021, 07:52 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020