BeautifulSoup接口介绍之2(提取文本内容方法)

上一节讲了查找标签,如果已经找到了标签,则继续提取其文本。

提取文本的常用方法是get_text,还有.string及.contents等等。

1. get_text() —— 提取标签及其所有子标签的文本**

get_text方法返回当前标签及其所有子标签的文本(合并成一个字符串)。可以指定分隔符和去除空白。

参数说明

示例

from bs4 import BeautifulSoup

html = '''
<div>
    中国人
    <span>北京</span>
    <span>上海</span>
</div>
'''

soup = BeautifulSoup(html, 'html.parser')
text = soup.div.get_text(separator=' ', strip=True)
print(text)  # 输出:'中国人 北京 上海'

2. .string —— 提取标签的直接文本(仅限单个文本节点)

特点

示例

```python from bs4 import BeautifulSoup

html = '''

中国人
中国

'''

soup = BeautifulSoup(html, 'html.parser')

情况1:标签内只有文本

print(soup.div.string) # 输出:'中国人'

情况2:标签内有嵌套标签

print(soup.find_all('div')[1].string) # 输出:None ```


3. .strings —— 生成所有子文本节点的迭代器

特点

示例

```python from bs4 import BeautifulSoup

html = '''

中国人 北京 上海

'''

soup = BeautifulSoup(html, 'html.parser') for s in soup.div.strings: print(repr(s)) # 输出:'\n 中国人\n '、'北京'、'\n '、'上海'、'\n' ```


4. .stripped_strings —— 生成去空白后的文本迭代器

特点

示例

```python from bs4 import BeautifulSoup

html = '''

中国人 北京 上海

'''

soup = BeautifulSoup(html, 'html.parser') for s in soup.div.stripped_strings: print(s) # 输出:'中国人'、'北京'、'上海' ```


5. .contents —— 获取标签的直接子节点列表

特点

示例

```python from bs4 import BeautifulSoup

html = '''

中国人 北京 上海

'''

soup = BeautifulSoup(html, 'html.parser') for child in soup.div.contents: print(repr(child)) # 输出:'\n 中国人\n '、北京、'\n '、上海、'\n' ```


6. .children —— 获取直接子节点的迭代器

特点

示例

```python from bs4 import BeautifulSoup

html = '''

中国人 北京 上海

'''

soup = BeautifulSoup(html, 'html.parser') for child in soup.div.children: print(repr(child)) # 输出同 .contents ```


7. .descendants —— 递归获取所有子孙节点

特点

示例

```python from bs4 import BeautifulSoup

html = '''

中国人 北京 上海

'''

soup = BeautifulSoup(html, 'html.parser') for descendant in soup.div.descendants: print(repr(descendant))

输出:'\n 中国人\n '、北京、'北京'、'\n '、上海、'上海'、'\n'

```


8. .parent / .parents —— 获取父节点

特点

示例

```python from bs4 import BeautifulSoup

html = '''

北京

'''

soup = BeautifulSoup(html, 'html.parser') span = soup.span print(span.parent.name) # 输出:'div' for parent in span.parents: print(parent.name) # 输出:'div'、'body'、'html'、'[document]' ```


9. .next_sibling / .previous_sibling —— 获取兄弟节点

特点

示例

```python from bs4 import BeautifulSoup

html = '''

A B C

'''

soup = BeautifulSoup(html, 'html.parser') span_a = soup.find('span', string='A')

print(spana.nextsibling) # 输出:'\n '(空白) print(spana.nextsibling.next_sibling) # 输出:B ```


10. .next_element / .previous_element —— 按文档顺序遍历节点

特点

示例

```python from bs4 import BeautifulSoup

html = '''

AB

'''

soup = BeautifulSoup(html, 'html.parser') span = soup.span

print(span.nextelement) # 输出:'A' print(span.nextelement.next_element) # 输出:'B' ```


方法对比总结

| 方法 | 返回类型 | 特点 | 适用场景 | |------|---------|------|---------| | get_text() | str | 提取所有文本(含子标签) | 需要合并文本 | | .string | strNone | 仅提取直接文本(无嵌套) | 单层文本 | | .strings | 生成器 | 生成所有子文本(含空白) | 逐条处理文本 | | .stripped_strings | 生成器 | 生成去空白后的子文本 | 清理后的文本 | | .contents | list | 直接子节点列表 | 按索引访问子节点 | | .children | 迭代器 | 直接子节点的迭代器 | 遍历子节点 | | .descendants | 生成器 | 所有子孙节点 | 递归遍历 | | .parent / .parents | Tag / 生成器 | 获取父节点/祖先节点 | 向上查找 | | .next_sibling / .previous_sibling | TagNone | 兄弟节点 | 水平查找 | | .next_element / .previous_element | Tagstr | 按文档顺序遍历 | 低层操作 |


如何选择?

  1. 提取所有文本get_text()
  2. 提取无嵌套的纯文本.string
  3. 逐条处理子文本.stripped_strings
  4. 遍历子节点.children.contents
  5. 递归遍历所有节点.descendants
  6. 查找父节点或兄弟节点.parent / .next_sibling

根据需求选择最合适的方法!