从 None 引发 JSONDecodeError("Expecting value", s, err.value)

问题描述

我正在运行我的代码以从基于 RNAcentral 登录号的 RNA 中央数据库中提取所需的数据。

有时在中间,程序停止并开始显示此错误:

获得错误


  File "C:\Users\soura\Desktop\Thesis\Saurav Data\Data_Extraction_RNA_Central.py",line 53,in <module>
    data = page.json()['results']

  File "D:\programs\anaconda3\lib\site-packages\requests\models.py",line 900,in json
    return complexjson.loads(self.text,**kwargs)

  File "D:\programs\anaconda3\lib\json\__init__.py",line 357,in loads
    return _default_decoder.decode(s)

  File "D:\programs\anaconda3\lib\json\decoder.py",line 337,in decode
    obj,end = self.raw_decode(s,idx=_w(s,0).end())

  File "D:\programs\anaconda3\lib\json\decoder.py",line 355,in raw_decode
    raise JSONDecodeError("Expecting value",s,err.value) from None

JSONDecodeError: Expecting value

这是我代码中出现错误的部分

import requests
import time

ids = ['URS0000D57BCE','URS0000D57BCE','URS0000EEF870','URS00009C33DE']

for id in range(len(ids)):
    # Accessing the database cross-reference url with exception handling and extracting info
    page = ''
    while page == '':

        try:
            page = requests.get('http://rnacentral.org/api/v1/rna/{}/xrefs.json'.format(ids[id]))
            break  
        except:
            print('Connection refused by the server at id {} and position {}'.format(ids[id],ids.index(ids[id])))
            print('Lets me sleep for 5 seconds')
            time.sleep(5)
            continue
        
    # Extracting json content from above url
    data = page.json()['results']

后来我像这样修改了我的代码,但仍然出现同样的错误:

我将 page = '' 改为 page = None 我认为因为错误是针对页面的 None 值。我就这样写了一个while循环,直到page的None值出现,代码就会一遍遍地重新运行。

import requests
import time

ids = ['URS0000D57BCE','URS00009C33DE']

for id in range(len(ids)):
    # Accessing the database cross-reference url with exception handling and extracting info
    page = None
    while page == None:

        try:
            page = requests.get('http://rnacentral.org/api/v1/rna/{}/xrefs.json'.format(ids[id]))
            break  
        except:
            print('Connection refused by the server at id {} and position {}'.format(ids[id],ids.index(ids[id])))
            print('Lets me sleep for 5 seconds')
            time.sleep(5)
            continue
        
    # Extracting json content from above url
    data = page.json()['results']

现在,我收到此错误:

Traceback (most recent call last):
  File "Data_Extraction_RNA_Central.py",line 61,in <module>
    data = page.json()['results']
  File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py",line 898,**kwargs)
  File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\__init__.py",line 348,in loads
    return _default_decoder.decode(s)
  File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\decoder.py",0).end())
  File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\decoder.py",err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

请如果有人可以帮助我,这对我来说将是一个很大的帮助。 :-)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)