使用BeautifulSoup4在<div>部分中获取带/不带<p>标记的字符串

2024-09-27 23:27:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个使用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>。在


Tags: anddivid顺序isfindthisps
3条回答

下面的方法似乎有效

import bs4

soup = bs4.BeautifulSoup("""
<div id='essay'>
this is paragraph1
<p>this is paragraph2</p>
this is paragraph3
<p>this is paragraph4</p>
</div>
""", "lxml")

main = soup.find('div', id='essay')
for child in main.children:
    print(child.string)

BeautfulSoup4还有递归模式,默认情况下是启用的。在

from bs4 import BeautifulSoup
html = """
<div id='essay'>
  this is paragraph1
  <p>this is paragraph2</p>
  this is paragraph3
  <p>this is paragraph4</p>
</div>
"""

soup = BeautifulSoup(html, "html.parser")
r = soup.find('div', id="essay", recursive=True).text
print (r)

很适合我。 尝试使用pip更新beauthoulsoup4。在

如果列表的长度相同,那么将它们交错起来可能会更容易,而不是编写代码用漂亮的汤来绕过原始格式

from itertools import chain

list_a = ['this is paragraph1', 'this is paragraph3']
list_b = ['this is paragraph2', 'this is paragraph4']

print(list(chain.from_iterable(zip(list_a, list_b))))


# ['this is paragraph1', 'this is paragraph2', 'this is paragraph3', 'this is paragraph4']

更多信息:Interleaving Lists in Python

相关问题 更多 >

    热门问题