[英]Convert website JSON data to CSV python, returns JSON decode error
I am trying to convert the JSON at this URL: https://wtrl.racing/assets/js/miscttt170620.php?wtrlid=63 , which I have saved in a file, to a CSV using this code:
json_data_file = open('TTT json', 'r')
content = json.load(json_data_file)
csv_results = csv.writer(open("TTT_results.csv.csv", "w", newline=''))
for item in content:
print(item)
csv_results.writerow(item)
这将返回: json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 489351 (char 489350)
,也就是 JSON 的这一部分中的 '2' "ll": 43.529}, {"aa":
我很困惑为什么会这样。
看起来 json 格式不正确。 您需要联系生成此 json 的人员进行修复。
查看条目:
"cc": "Nick "Lionel" Berry(TriTalk)"
"cc": ""Sherpa" Dave (R&K Hyenas)"
这些引号没有正确转义。 它需要是:
"cc": "Nick \"Lionel\" Berry(TriTalk)"
"cc": "\"Sherpa\" Dave (R&K Hyenas)"
看起来您在访问数据时可能请求了错误的接受编码。 尝试通过指定您想要 JSON 响应而不是 HTML 有效负载来下载它:
import requests
url = 'https://wtrl.racing/assets/js/miscttt170620.php?wtrlid=63'
headers = {'Accept': 'application/json'}
response = requests.get(url, headers=headers)
data = response.json()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.