如:
<div>
<p>内容一<p>内容二</p>内容三<p>内容四</p>内容五<p>内容六</p>内容七<p>内容八</p>.........</p>
</div>
我该怎么做才能按顺序获取所有内容呢?期望得到的数据:
内容一
内容二
内容三
内容四
内容五
内容六
内容七
内容八
......
谢谢大家
如:
<div>
<p>内容一<p>内容二</p>内容三<p>内容四</p>内容五<p>内容六</p>内容七<p>内容八</p>.........</p>
</div>
我该怎么做才能按顺序获取所有内容呢?期望得到的数据:
内容一
内容二
内容三
内容四
内容五
内容六
内容七
内容八
......
谢谢大家
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
html_doc = '''<div>
<p>内容一<p>内容二</p>内容三<p>内容四</p>内容五<p>内容六</p>内容七<p>内容八</p>.........</p>
</div>'''
soup = BeautifulSoup(html_doc, 'lxml')
# method 1
tags = soup.find('div').find_all('p')
for i in tags:
# do something here
print i.text
# method 2
print soup.find('div').find('p').text
print soup.find('div').find('p').find_next('p').text
2 回答4.3k 阅读✓ 已解决
2 回答864 阅读✓ 已解决
1 回答4.1k 阅读✓ 已解决
3 回答866 阅读✓ 已解决
2 回答2.2k 阅读✓ 已解决
4 回答2.6k 阅读
3 回答908 阅读✓ 已解决
题主提供的的内容<p>标签有点乱