I am trying to make my code print all the plain text from a website. This is my code:
import requests
import json
response = requests.get("https://example.com")
json_data = json.loads(response.text)
print(str(json_data))
For example: if I enter https://example.com
, I want to make the program write
Example Domain
This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.
More information...
But I get this error message:
Traceback (most recent call last):
File "main.py", line 4, in <module>
json_data = json.loads(response.text)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
How can I fix this?
json.loads()
? Website responses typically aren't in json format.