【发布时间】:2020-06-17 12:05:37
【问题描述】:
我有一个包含多个 Div 类/跨度类的 XML,我正在努力提取文本值。
<div class="line">
<span class="html-tag">
"This is a Heading that I dont want"
</span>
<span>This is the text I want</span>
到目前为止,我已经写了这个:
html = driver.page_source
soup = BeautifulSoup(html, "lxml")
spans = soup.find_all('span', attrs={'class': 'html-tag'})[29]
print(spans.text)
不幸的是,这只会打印出“这是我不想要的标题”值,例如
This is the heading I dont want
代码中的数字[29]是我需要的文字总会出现的位置。
我不确定如何检索我需要的跨度值。
请您帮忙。谢谢
【问题讨论】:
-
如果你想要的标签没有这个类,为什么要搜索attrs={'class': 'html-tag'}?
标签: python xml beautifulsoup