从 UniProt 数据库中抓取 table

Web scraping table from UniProt database

我有一个 UniProt ID 列表,想使用 BeautifulSoup 废弃一个包含结构信息的 table。我使用的url如下:https://www.uniprot.org/uniprot/P03496,登录号为“P03496”。

html代码片段如下。

<div class="main-aside">
    <div class="content entry_view_content up_entry swissprot">
        <div class="section" id="structure">
            <protvista-uniprot-structure accession="P03468">
                <div class="protvista-uniprot-structure">
                    <div class="class=" protvista-uniprot-structure__table">
                        <protvista-datatable class="feature">
                            <table>...</table>
                        </protvista-datatable>
                    </div>
                </div>
            </protvista-uniprot-structure>
        </div>
    </div>
</div>

我需要的信息在<table>...</table>标签之间。

我试过了

from bs4 import BeautifulSoup
import requests

url='https://www.uniprot.org/uniprot/P03468'
r=requests.get(url)
url=r.content
soup = BeautifulSoup(url,'html.parser')
soup.find("protvista-datatable", {"class": "feature"})
print(soup)

内容是动态提供的,不包含在您的 soup 中(如果您深入了解的话)。它不需要 BeautifulSoup 来获取数据,你的 tabel 是基于,只需使用他们的 api / rest 接口来获取结构化数据,如 JSON:

import requests
url='https://rest.uniprot.org/uniprot/P03468'
## fetch the json response
data = requests.get(url).json()
## pick needed data e.g. 
data['uniProtKBCrossReferences']

输出

[{'database': 'EMBL',
  'id': 'J02146',
  'properties': [{'key': 'ProteinId', 'value': 'AAA43412.1'},
   {'key': 'Status', 'value': '-'},
   {'key': 'MoleculeType', 'value': 'Genomic_RNA'}]},
 {'database': 'EMBL',
  'id': 'AF389120',
  'properties': [{'key': 'ProteinId', 'value': 'AAM75160.1'},
   {'key': 'Status', 'value': '-'},
   {'key': 'MoleculeType', 'value': 'Genomic_RNA'}]},
 {'database': 'EMBL',
  'id': 'EF467823',
  'properties': [{'key': 'ProteinId', 'value': 'ABO21711.1'},
   {'key': 'Status', 'value': '-'},
   {'key': 'MoleculeType', 'value': 'Genomic_RNA'}]},
 {'database': 'EMBL',
  'id': 'CY009446',
  'properties': [{'key': 'ProteinId', 'value': 'ABD77678.1'},
   {'key': 'Status', 'value': '-'},
   {'key': 'MoleculeType', 'value': 'Genomic_RNA'}]},
 {'database': 'EMBL',
  'id': 'K01031',
  'properties': [{'key': 'ProteinId', 'value': 'AAA43415.1'},
   {'key': 'Status', 'value': '-'},
   {'key': 'MoleculeType', 'value': 'Genomic_RNA'}]},
 {'database': 'RefSeq',
  'id': 'NP_040981.1',
  'properties': [{'key': 'NucleotideSequenceId', 'value': 'NC_002018.1'}]},
 {'database': 'PDB',
  'id': '6WZY',
  'properties': [{'key': 'Method', 'value': 'X-ray'},
   {'key': 'Resolution', 'value': '1.50 A'},
   {'key': 'Chains', 'value': 'C=181-190'}]},...]