2

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

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

获得的错误


  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循环,直到页面的None值,代码将一次又一次地重新运行。

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 = 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, in json
    return complexjson.loads(self.text, **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", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\soura\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

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

4

0 回答 0