python - 使用 BeautifulSoup 提取标签值

标签 python parsing tags beautifulsoup

有人可以指导我如何使用 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/

相关文章:

python - 使用 str(count) 追加到列表会出错

Python/Pandas 设置 y 轴属性

c# - 如何在 Console.WriteLine() 语句中将数字转换为字母

c++ - 如何在 C++ 中解析表达式

php - 如何在php中显示来自mysql数据库的html

c++ - 在菱形继承(钻石问题)中更喜欢一个类的变量

python - 访问 python 中的字典列表并使用值对其进行排序(嵌套字典)

python - 为什么在声明后设置class属性会在python中返回错误?

php - PHP解析/语法错误;以及如何解决它们

html - "href"在 HTML 中代表什么?