Как извлечь весь текст ниже заголовка c? В этом случае мне нужно извлечь текст под Topic 2
. РЕДАКТИРОВАТЬ: На других веб-страницах "Topi c 2" иногда появляется в качестве третьего заголовка или первого. «Topi c 2» не всегда находится в одном и том же месте и не всегда имеет одинаковый идентификационный номер.
# import library
from bs4 import BeautifulSoup
# dummy webpage text
body = '''
<h2 id="1">Topic 1</h2>
<p> This is the first sentence.</p>
<p> This is the second sentence.</p>
<p> This is the third sentence.</p>
<h2 id="2">Topic 2</h2>
<p> This is the fourth sentence.</p>
<p> This is the fifth sentence.</p>
<h2 id="3">Topic 3</h2>
<p> This is the sixth sentence.</p>
<p> This is the seventh sentence.</p>
<p> This is the eighth sentence.</p>
'''
# convert text to soup
soup = BeautifulSoup(body, 'lxml')
Если я извлекаю текст только под '' 'Topi c 2 '' ', это мой вывод.
This is the fourth sentence. This is the fifth sentence.
Мои попытки решить эту проблему:
Я пытался soup.select('h2 + p')
, но это только дайте мне первые предложения под каждым заголовком.
[<p> This is the first sentence.</p>,
<p> This is the fourth sentence.</p>,
<p> This is the sixth sentence.</p>]
Я тоже попробовал это, но он дал мне весь текст, когда мне нужен только текст под Topic 2
:
import pandas as pd
lst = []
for row in soup.find_all('p'):
text_dict = {}
text_dict['text'] = row.text
lst.append(text_dict)
df = pd.DataFrame(lst)
df
| | text |
|---|-------------------------------|
| 0 | This is the first sentence. |
| 1 | This is the second sentence. |
| 2 | This is the third sentence. |
| 3 | This is the fourth sentence. |
| 4 | This is the fifth sentence. |
| 5 | This is the sixth sentence. |
| 6 | This is the seventh sentence. |
| 7 | This is the eighth sentence. |