使用beautifulsoup进行解析以获得准确的单词

2024-04-29 03:15:42 发布

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

我想使用beautifulsoup提取所需的单词。对于我的应用程序,我使用arxivapi来获取搜索相关论文的总数。对于我的查询,我使用了electron进行搜索。API返回的总搜索结果约为144055。我想提取这个总数

import urllib.request as ur
from bs4 import BeautifulSoup

url = 'http://export.arxiv.org/api/query?search_query=all:electron' # arxiv:api
s = ur.urlopen(url)
sl = s.read()
soup = BeautifulSoup(sl, 'html.parser')
print(soup.prettify('latin-1'))
desire_word=soup.find('opensearch:totalresults')
print(desire_word)

我打印欲望字。但我是全文的

<opensearch:totalresults xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">144055</opensearch:totalresults>

我怎样才能得到总数(144055)


Tags: importapihttpurlarxivopensearchqueryprint
2条回答

没有足够的声誉来评论,但是当你遇到不确定返回什么的情况时需要注意:

print(type(desire_word))
print(dir(desire_word))

这样你就能看到了

1)单词不是字符串,而是元素

2)desire_word有一个名为“text”和“听起来很有用,让我试试desire_word.text”的属性

你很接近

print(desire_word.text)

相关问题 更多 >