簡體   English   中英

python函數json.loads():無法解碼JSON對象

[英]python function, json.loads() : No JSON object could be decoded

代碼很簡單:

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

誰能幫我解決這個問題? 謝謝!

此代碼將幫助您弄清楚發生了什么;)

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)
    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM