python - 如何使用 BeautifulSoup bs4 获取 HTML 标签的内部文本值?

标签 python html beautifulsoup

当使用 BeautifulSoup bs4 时,如何从 HTML 标签中获取文本?当我运行这一行时:

oname = soup.find("title")

我得到这样的 title 标签:

<title>page name</title>

现在我只想获取它的内部文本,页面名称,不带标签。如何做到这一点?

最佳答案

使用 .text 从标签中获取文本。

oname = soup.find("title")
oname.text

或者只是soup.title.text

In [4]: from bs4 import BeautifulSoup    
In [5]: import  requests
In [6]: r = requests.get("http://stackoverflow.com/questions/27934387/how-to-retrieve-information-inside-a-tag-with-python/27934403#27934387")    
In [7]: BeautifulSoup(r.content).title.text
Out[7]: u'html - How to Retrieve information inside a tag with python - Stack Overflow'

要打开文件并使用文本作为名称,只需像使用任何其他字符串一样使用它:

with open(oname.text, 'w') as f

关于python - 如何使用 BeautifulSoup bs4 获取 HTML 标签的内部文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934387/

相关文章:

jquery - 返回顶部不关注必填字段

python - 如何在 Beautiful Soup 中深入多个级别(find_all 错误)

python - 为什么在我的字符串末尾添加换行符?

稀疏随机填充数组的 Pythonic 方法?

javascript - 如何在 js 数组中搜索?

javascript - bootstrap.min.css 文件不支持

python-3.x - 如何通过Python中的Beautiful Soup找到类中的文本和类名中的空格?

python - BeautifulSoup 迭代多个 XML 标签,提取字符串列表

migration - 将 Python 中格式良好的动态表格数据转换为 str.format()

python - 如何在 matplotlib 中设置纵横比?