如何使用python将HTML响应解析为json格式?

时间:2017-09-08 02:48:38

标签: python html json database

我使用python2向RNAcentral数据库发出请求,并使用此命令将响应读作JSON格式:response.json()

这让我将数据作为字典数据类型读取,因此我使用相应的语法从交叉引用中获取数据,其中包含一些指向其他数据库的链接,但是当我尝试使用上面提到的命令,我无法将其读作JSON,因为我只能以HTML格式获取响应内容。

所以我需要知道如何从交叉引用中读取每个链接的请求,并使用python语言将其作为JSON读取。

以下是代码:

direcc = 'http://rnacentral.org/api/v1/rna/'+code+'/?flat=true.json'
resp = requests.get(direcc)
datos=resp.json()
d={}
links = []
for diccionario in datos['xrefs']['results']:
    if diccionario['taxid']==9606:
    base_datos=diccionario['database']
    for llave,valor in diccionario['accession'].iteritems():
        d[base_datos]={'url':diccionario['accession']['url'],
        'expert_db_url':diccionario['accession']['expert_db_url'],
        'source_url':diccionario['accession']['source_url']}
for key,value in d.iteritems():
    links.append(d[key]['expert_db_url'])
for item in links:
    response = requests.get(item)
    r = response.json()

这是我得到的错误:ValueError:无法解码JSON对象。 谢谢。

0 个答案:

没有答案