下面是使用 BeautifulSoup 库中的 'text' 属性获取文本的示例代码:

from bs4 import BeautifulSoup

html = '''
<html>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph.</p>
    <a href='https://example.com'>This is a link</a>
</body>
</html>
'''

# 创建 BeautifulSoup 对象
soup = BeautifulSoup(html, 'html.parser')

# 使用 'text' 属性获取文本
text = soup.text

print(text)

输出结果为:

This is a heading
This is a paragraph.
This is a link

在上面的代码中,我们首先创建了一个 'BeautifulSoup' 对象,然后使用 'text' 属性获取了整个 HTML 文档中的文本内容。最后,我们打印出获取到的文本内容。

Python BeautifulSoup: 使用 text 属性提取文本

原文地址: https://www.cveoy.top/t/topic/piay 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录