如何获取.content中未包含的HTML标记中的文本?

2024-06-07 00:30:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把像这样的页面中的文本:https://www.ncbi.nlm.nih.gov/protein/p22217刮成一个字符串。尤其是DBSOURCE中的文本块

我似乎有很多关于使用的建议汤.findall(text=true)诸如此类,但却一无所获。至少在2018年之前的任何东西似乎都已经过时了(我使用的是python3.7)。我认为问题是我想要的内容超出了r.text和r.content的范围;当我用ctrl F搜索时,我要查找的部分不在那里。你知道吗

from bs4 import BeautifulSoup

import requests

url = "https://www.ncbi.nlm.nih.gov/protein/P22217"

r = requests.get(url)

data = r.content

soup = BeautifulSoup(data, "html.parser")

PageInfo = soup.find("pre", attrs={"class":"genbank"})

print(PageInfo)

这种尝试和其他尝试的结果是“无”。没有错误信息,它只是不返回任何东西。你知道吗


Tags: texthttps文本importurldatawwwncbi
2条回答

页面正在进行XHR调用以获取您要查找的信息。 调用是https://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?id=135747&db=protein&report=genpept&conwithfeat=on&show-cdd=on&retmode=html&withmarkup=on&tool=portal&log$=seqview&;maxdownloadsize=1000000

它又回来了

<div class="sequence">
<a name="locus_P22217.3"></a><div class="localnav"><ul class="locals"><li><a href="#comment_P22217.3" title="Jump to the comment section of this record">Comment</a></li><li><a href="#feature_P22217.3" title="Jump to the feature table of this record">Features</a></li><li><a href="#sequence_P22217.3" title="Jump to the sequence of this record">Sequence</a></li></ul></div>
<pre class="genbank">LOCUS       TRX1_YEAST               103 aa            linear   PLN 18-SEP-2019
DEFINITION  RecName: Full=Thioredoxin-1; AltName: Full=Thioredoxin I;
            Short=TR-I; AltName: Full=Thioredoxin-2.
ACCESSION   P22217
VERSION     P22217.3
**DBSOURCE**    UniProtKB: locus TRX1_YEAST, accession <a href="https://www.uniprot.org/uniprot/P22217">P22217</a>;
            class: standard.
            extra accessions:D6VY45
            created: Aug 1, 1991.

。。。你知道吗

因此,从代码中执行HTTP调用以获取数据。你知道吗

您可以使用它,因为页面依赖于xmlhttprequests

代码:

from bs4 import BeautifulSoup

import requests,re

url = "https://www.ncbi.nlm.nih.gov/protein/P22217"

r = requests.get(url)
soup = BeautifulSoup(r.content,features='html.parser')
pageId = soup.find('meta', attrs={'name':'ncbi_uidlist'})['content']

api = requests.get('https://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?id={}'.format(pageId))

data = re.search(r'DBSOURCE([\w\s\n\t.:,;()-_]*)KEYWORD',api.text)
print(data.group(1).strip())

演示代码:Here

说明:

  • 对url的请求将有助于获取您所请求的产品的id,该id存在于页面的meta中。你知道吗
  • 通过获取id,第二个请求将使用websiteapi来获取所需的描述。正则表达式模式将用于分隔想要的部分和不想要的部分。你知道吗

正则表达式:

DBSOURCE([\w\s\n\t.:,;()-_]*)KEYWORD

演示正则表达式:Here

相关问题 更多 >