簡體   English   中英

Python無法讀取有效的JSON

[英]Python not reading valid JSON

我正在從網頁上抓取一些HTML源以提取以json格式存儲的數據

這是代碼:

url = 'https://finance.yahoo.com/quote/SPY'
result = requests.get(url)

c = result.content
html = BeautifulSoup(c, 'html.parser')
scripts = html.find_all('script')

sl =[]
for s in scripts:

     sl.append(s)

s = (sl[-3])
s = s.contents
s = str(s)
s = s[119:-16]

json_data = json.loads(s)

運行以上操作會引發此錯誤:

json.decoder.JSONDecodError: Expecting ',' delimiter: line 1 column 7506 (char7505)

當我獲取變量s的內容並將其傳遞給json格式化程序時,它將被識別為正確的json。

我使用以下網站檢查json: http : //jsonprettyprint.com/json-pretty-printer.php

為什么在Python中使用json.loads()時會出現此錯誤? 我是否認為這與字符串編碼不正確或轉義字符的存在有關?

我該如何解決?

您的JSON包含某些意外令牌,例如true 首先使用json.dumps來解決它。

print (json.dumps(s,indent =2))
s = json.dumps(s)
json_data = json.loads(s)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)

使用失敗消息,您可以打印字符串的一部分以查看失敗的地方。

print(s[7400:7500])
mailboxes.isPrimary=\\"true\\" AND ymreq

如skaul05所述,由於字符串中的true標記,它失敗了。

import requests
from bs4 import BeautifulSoup
import json

url = 'https://finance.yahoo.com/quote/SPY'
result = requests.get(url)

c = result.content
html = BeautifulSoup(c, 'html.parser')
scripts = html.find_all('script')

sl =[]
for s in scripts:

     sl.append(s)

s = (sl[-3])
s = s.contents

a = s[0][111:-12]

jjjj = json.loads(a)

處理列表時出現問題,只需使用str()

如果它是有效的JSON格式的文本,則解析器不會抱怨。 這就是我測試的方式

//first I scraped that page
curl https://finance.yahoo.com/quote/SPY > SPY.json
//then tried to parse it using json
a = open("SPY.json")
b = json.load(a)
ValueError: No JSON object could be decoded

您可能需要先將其解析為有效的xml。

暫無
暫無

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

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