我刚开始学习在Python中使用BeautifulSoup解析html,有一个非常简单的愚蠢问题。不知何故,我只是无法从下面的html(存储在容器中)中获取文本1
....
<div class="listA">
<span><span>Text 1</span><b>Text 2</b><b>Text 3</b></span>
</div>
...
soup = BeautifulSoup(driver.page_source, 'html.parser')
containers = soup.findAll("div", {"class": "listA"})
datas = []
for data in containers:
textspan = data.find("span")
datas.append(textspan.text)
输出如下:Text1Text2Text3
有没有什么建议来界定它们呢?谢谢,非常感谢
另一个解决方案涉及simplifieddoc,它不依赖第三方库,而且更轻、更快,非常适合初学者。 这里有更多的例子here
结果:
如果您只想文本1使用此代码
输出:
相关问题 更多 >
编程相关推荐