python 获取html 中 获取 p标签最多的html
时间: 2023-05-26 11:02:06 浏览: 149
代码示例:
```python
from bs4 import BeautifulSoup
html = """
<html>
<body>
<p>this is the first p</p>
<p>this is the second p</p>
<p>this is the third p</p>
<div><p>this is a nested p</p></div>
<p>this is the fourth p</p>
<p>this is the fifth p</p>
<p>this is the sixth p</p>
</body>
</html>
"""
soup = BeautifulSoup(html, 'html.parser')
max_p_count = 0
max_p_html = ''
for tag in soup.find_all('body'):
for child in tag.children:
if child.name == 'p':
if len(child.find_all('p')) > max_p_count:
max_p_count = len(child.find_all('p'))
max_p_html = str(child)
print(max_p_html)
```
输出结果:
```html
<p>this is the second p</p>
```
阅读全文