以下是使用BeautifulSoup提取
标签的内部内容的代码示例:
from bs4 import BeautifulSoup
# HTML代码
html = '''
This is a paragraph.
This is a span.
This is a div.
'''
# 创建BeautifulSoup对象
soup = BeautifulSoup(html, 'html.parser')
# 查找所有的
标签
br_tags = soup.find_all('br')
# 提取每个
标签的内部内容
for br_tag in br_tags:
content = br_tag.next_sibling.strip()
print(content)
运行以上代码,将会输出以下结果:
This is a span.
This is a div.
注意:next_sibling
返回的是下一个兄弟节点,strip()
用于去除字符串两侧的空格和换行符。