有人可以指导我如何使用 BeautifulSoup 提取标签的值吗?我阅读了文档,但很难浏览它。例如,如果我有:
<span title="Funstuff" class="thisClass">Fun Text</span>
我如何才能通过 BeautifulSoup/Python 提取“Funstuff”?
编辑:我使用的是 3.2.1 版
最佳答案
你需要有一些东西来识别你正在寻找的元素,而在这道题中很难说出它是什么。
例如,这两个都将在 BeautifulSoup 3 中打印出“Funstuff”。一个查找 span 元素并获取标题,另一个查找具有给定类的 span。达到这一点的许多其他有效方法也是可能的。
import BeautifulSoup
soup = BeautifulSoup.BeautifulSoup('<html><body><span title="Funstuff" class="thisClass">Fun Text</span></body></html>')
print soup.html.body.span['title']
print soup.find('span', {"class": "thisClass"})['title']
关于python - 使用 BeautifulSoup 提取标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11618433/