2017-07-12 85 views
1

運行時腳本有時錯誤想出:JSON蟒錯誤:期待值:第1行第1列(CHAR 0)

Traceback (most recent call last): 
    File "C:\Users\dzwon\Desktop\osu_pp_watch_v15_FINAL.py", line 27, in <module> 
    data=(requests.get('https://osu.ppy.sh/api/get_user?k=' + APIkey + '&u=' + username)).json() 
    File "C:\Users\dzwon\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\models.py", line 894, in json 
    return complexjson.loads(self.text, **kwargs) 
    File "C:\Users\dzwon\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads 
    return _default_decoder.decode(s) 
    File "C:\Users\dzwon\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
    File "C:\Users\dzwon\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode 
    raise JSONDecodeError("Expecting value", s, err.value) from None 
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

錯誤可在15或2分鐘後彈出或在所有

不會彈出

腳本是解析JSON

data=(requests.get('https://osu.ppy.sh/api/get_user?k=' + APIkey + '&u=' + username)).json() 
pp = data[0]['pp_raw'] 
rank = data[0]['pp_rank'] 

的部分和JSON數據

[{"user_id":"5390233","username":"dzwon21","count300":"439843","count100":"39207","count50":"5245","playcount":"3860","ranked_score":"753280488","total_score":"1708595594","pp_rank":"223986","level":"63.7695","pp_raw":"842.426","accuracy":"97.04467010498047","count_rank_ss":"53","count_rank_s":"235","count_rank_a":"264","country":"PL","pp_country_rank":"10947","events":[{"display_html":"<b><a href='\/u\/5390233'>dzwon21<\/a><\/b> unlocked the \"<b>Insanity Approaches<\/b>\" medal!","beatmap_id":"0","beatmapset_id":"0","date":"2017-07-12 17:43:17","epicfactor":"4"}]}] 
+0

這可能是您的請求不時返回有效JSON以外的內容。嘗試記錄從服務器收到的內容,然後嘗試對其進行解碼或發生錯誤。 –

+0

我會嘗試和張貼,如果出現錯誤 – dzwon21

回答

2

您的HTTP請求失敗。在抓取JSON數據之前,您應該檢查HTTP請求的成功或失敗:

#UNTESTED 
result=requests.get('https://osu.ppy.sh/api/get_user?k=' + APIkey + '&u=' + username) 
if result.ok and result.headers['content-type'] == 'application/json': 
    data = result.json() 
else: 
    raise SomeError("HTTP did not return data.") 

pp = data[0]['pp_raw'] 
rank = data[0]['pp_rank'] 
+0

因此,我編輯了一點點的代碼,並把它放在代碼沒有檢測到http請求失敗。新錯誤https://pastebin.com/nC2US6yF – dzwon21

+0

我修好了,謝謝代碼 – dzwon21

相關問題