代码非常简单:
import requests
import json
r = requests.get('https://www.baidu.com/')
r.encoding = 'utf-8'
json.loads(r.text,'utf-8')
我总是收到此错误信息:
Traceback (most recent call last):
File "<pyshell#57>", line 1, in <module>
json.loads(r.text,'utf-8')
File "C:\Python27\lib\json\__init__.py", line 352, in loads
return cls(encoding=encoding, **kw).decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
任何人都可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:0)
此代码将帮助您弄清楚正在发生的事情;)
import requests
import json
r = requests.get('https://www.baidu.com/')
r.encoding = 'utf-8'
try:
foo = json.loads(r.text, 'utf-8')
print "Yay, I got a json from baidu!"
except Exception, e:
print "Why didn't i get a json from baidu? Maybe it wasn't a json..."
print "What is it then? It seems is a {0} whose length is {1}".format(
r.text.__class__, len(r.text)
)