我已经用python编写了一个脚本来从URL获得响应,以便以后可以使用它。我希望得到的响应是json。但是,我得到的却更像是json:
{"DISPLAY":{"ADA":{"EUR":{"PRICE":"€ 0.07575","SPRICE":"€ 0.08","CHANGE24HOUR":"€ -0.011","CHANGEPCT24HOUR":"-12.58","CLOSED":"€ 0.08","DAY":-12.290825158684369,"HIGH24HOUR":"€ 0.087","LOW24HOUR":"€ 0.072","MKTCAP":"€ 1963975593.25","MKTCAP_ROUND":"€ 1.96 B","MONTH":27.26481127968117,"OPEN24HOUR":"€ 0.08665","SUPPLY":"ADA 25.93 B","TIMESTAMP":1559305128,"VOLUME24HOUR":"ADA 22.74 M","VOLUME24HOURTO":"€ 1.78 M","WEEK":9.685520068120473,"DATA_PROVIDER":2}
到目前为止,我已经写过:
import json
import requests
url = 'https://ticker.cointelegraph.com/tickers'
res = requests.get(url)
print(res.text)
#the following line throws an error
print(json.loads(res.text))
使用json.loads()
,出现以下错误:
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 14796 (char 14795)
如何从该网址获取纯json内容?
答案 0 :(得分:3)
如果您查看function CreateObject(type: 'typeA' | 'typeB'): VariantA
function CreateObject(type: 'typeA' | 'typeB'): VariantB
function CreateObject(type: 'typeA' | 'typeB'): VariantA | VariantB {
if (type === 'typeA') {
return { a: 1, b: '2', c: ['3', '4'] } as VariantA
} else {
return { e: 1, f: '2', g: ['3', '4'] } as VariantB
}
}
的内容或页面res.text
的内容,则字符串中有一个https://ticker.cointelegraph.com/tickers
会导致错误,可能是将其替换为可分析的对象,例如字符串+Inf
或数字+Inf
0
然后import json
import requests
url = 'https://ticker.cointelegraph.com/tickers'
res = requests.get(url)
#Replaced the +Inf with +Inf string
json_obj = json.loads(res.text.replace('+Inf','"+Inf'))
#Or replace the +Inf with 0 string
#json_obj = json.loads(res.text.replace('+Inf','0'))
将具有您的预期输出