要使用BeautifulSoup查找嵌套标签,可以按照以下步骤进行操作:
from bs4 import BeautifulSoup
html_doc = """
Example
Header 1
Paragraph 1
Paragraph 2
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# 查找第一个标签
div_tag = soup.find('div')
# 查找标签下的第一个标签
h1_tag = div_tag.find('h1')
# 查找所有
标签
p_tags = soup.find_all('p')
# 查找所有带有class为"text"的
标签
text_p_tags = soup.find_all('p', class_='text')
- 打印或处理查找到的内容:
print(div_tag)
print(h1_tag)
print(p_tags)
print(text_p_tags)
完整的示例代码如下:
from bs4 import BeautifulSoup
html_doc = """
Example
Header 1
Paragraph 1
Paragraph 2
"""
soup = BeautifulSoup(html_doc, 'html.parser')
div_tag = soup.find('div')
h1_tag = div_tag.find('h1')
p_tags = soup.find_all('p')
text_p_tags = soup.find_all('p', class_='text')
print(div_tag)
print(h1_tag)
print(p_tags)
print(text_p_tags)
运行以上代码,你将会得到以下输出:
Header 1
Paragraph 1
Paragraph 2
Header 1
[Paragraph 1
, Paragraph 2
]
[Paragraph 1
]
相关内容