【发布时间】:2022-01-15 20:38:30
【问题描述】:
我正在尝试让我的代码打印网站上的所有纯文本。这是我的代码:
import requests
import json
response = requests.get("https://example.com")
json_data = json.loads(response.text)
print(str(json_data))
例如:如果我输入https://example.com,我想让程序写
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...
但我收到此错误消息:
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)
我该如何解决这个问题?
【问题讨论】:
-
你为什么打电话给
json.loads()?网站响应通常不是 json 格式。
标签: python json web python-requests