在抓取 json 檔案串列時,有時檔案丟失且無法下載。在我的 python 腳本中,當這種情況發生時,腳本會顯示錯誤
json.decoder.JSONDecodeError : 期望值:linke 1 column 1 (char 0)
如果出現錯誤,我如何要求腳本繼續回圈?我嘗試放入并嘗試:除了,但沒有成功(IndentationError)
這是代碼:
RACE_L = x1["pageProps"]["initialState"]["racecards"]["races"][today2]
for r1 in RACE_L:
id_race = r1["uuid"]
link2go = link_append id_race '.json'
n1 = "races"
n12 = "races"
n2 = r1["uuid"]
name1 = n12 '-' n2
name1 = today2 '_' name1 '.json'
with open(path '%s' %name1,'w',encoding='utf-8') as f2:
print('Writing %s into file' %name1)
r3 = requests.get(link2go, headers=headers)
sleep(2)
x3 = r3.json()
json.dump(x3, f2, indent=4, ensure_ascii=False)
uj5u.com熱心網友回復:
以這種方式放置 try 和 except 塊-
RACE_L = x1["pageProps"]["initialState"]["racecards"]["races"][today2]
for r1 in RACE_L:
try:
id_race = r1["uuid"]
link2go = link_append id_race '.json'
n1 = "races"
n12 = "races"
n2 = r1["uuid"]
name1 = n12 '-' n2
name1 = today2 '_' name1 '.json'
with open(path '%s' %name1,'w',encoding='utf-8') as f2:
print('Writing %s into file' %name1)
r3 = requests.get(link2go, headers=headers)
sleep(2)
x3 = r3.json()
json.dump(x3, f2, indent=4, ensure_ascii=False)
except:
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367727.html
上一篇:如何在pyton中使用特殊字符?
