首页 > 解决方案 > 如何从 bs4.element.Tag 中获取值

问题描述

我正在与BeautifulSoup一个网络抓取项目合作。我有一个标签:

<span class="sal "><em class="iconRup"></em>40.0 Lacs</span>

我想从中检索40.0 Lacs

我尝试使用:

salary = soup.find('span', class_ = 'sal')
print(salary.string())

但这给出了一个None object. 我该如何进行?

标签: python-3.xweb-scrapingbeautifulsoup

解决方案


这是如何:

from bs4 import BeautifulSoup

html = """
<span class="sal "><em class="iconRup"></em>40.0 Lacs</span>
"""

soup = BeautifulSoup(html, "html.parser").find('span', class_='sal')
print(soup.getText())

输出:

40.0 Lacs

推荐阅读