我有一个使用BS4的旧网页要废弃。其中一部分是一篇我需要删掉的长篇文章。那篇文章的格式很奇怪,像这样:
<div id='essay'>
this is paragraph1
<p>this is paragraph2</p>
this is paragraph3
<p>this is paragraph4</p>
</div>
使用bs4,我尝试了以下操作: 使用
^{pr2}$我能提取
'this is paragraph1' and 'this is paragraph3'
或者
ps = soup.find('div', id='essay').find_all('p')
for p in ps:
print p.text
我能提取
'this is paragraph2' and 'this is paragraph4'
如果我同时使用这两个,我会得到第1、3、2、4段,这是不正常的。我需要确保段落顺序也是正确的。我能做些什么来达到这个目的呢?在
编辑:问题只是一个例子,它不保证在偶数和奇数段之间交错。。。让我稍微澄清一下我的问题:我想有一种方法来按顺序提取段落,而不管是否有<;p>。在
下面的方法似乎有效
BeautfulSoup4还有递归模式,默认情况下是启用的。在
很适合我。 尝试使用pip更新beauthoulsoup4。在
如果列表的长度相同,那么将它们交错起来可能会更容易,而不是编写代码用漂亮的汤来绕过原始格式
更多信息:Interleaving Lists in Python
相关问题 更多 >
编程相关推荐