美丽汤:在定义的 h2 标签之间拉 p 标签



这让我有点困惑了。我正在尝试按"新资金"和"新基金"的名称从"h2"标签下的"p"标签中提取所有文本。 每个页面的"p"标签数量不一致,所以我在考虑某种 while 循环,但我尝试的不起作用。每个标签的格式通常是带有"强">

的公司名称,然后列出文本和其他"强"标签,说明谁资助/投资。

一旦我可以正确解析它,目标是从"强"标签导出公司名称,其中包含后续文本和投资公司/人员(从跟随"p"块中的"强"标签进行一些数据分析。

任何帮助将不胜感激 - 是的,我已经浏览了其他各种帮助页面,但我所做的尝试没有成功,所以我来到了这里。

import requests
page = requests.get("https://www.strictlyvc.com/2017/06/13/strictlyvc-june-12-2017/")
page
page.content
from bs4 import BeautifulSoup
soup = BeautifulSoup(page.content, 'html.parser')
entrysoup = soup.find(class_ = 'post-entry')

试图拉出正确的段落,但这些段落只选择下一个段落,我想要"新资金">

和"新资金"下的所有标签(基本上,直到下一个标签不是这两个

print(entrysoup.find('h2', text = 'New Fundings').find_next_sibling('p'))
print(entrysoup.find('h2', text = 'New Funds').find_next_sibling('p'))

这更接近,但我不确定当它击中非新资金/新资金标签时如何让它停止

for strong_tag in entrysoup.find_all('strong'):
print (strong_tag.text, strong_tag.next_sibling)

我认为这是我目前能得到的最好的结果。 如果这不是你想要的,请告诉我,这样我就可以摆弄更多。 如果是将其标记为答案:(

import requests
import bs4
page = requests.get("https://www.strictlyvc.com/2017/06/13/strictlyvc-june-12-2017/")
soup =bs4.BeautifulSoup(page.content, 'html.parser')
entrysoup = soup.find(class_ = 'post-entry')
Stop_Point = 'Also Sponsored By . . .'
for strong_tag in entrysoup.find_all('h2'):
if strong_tag.get_text() == 'New Fundings':
for sibling in strong_tag.next_siblings:
if isinstance(sibling, bs4.element.Tag):
print(sibling.get_text())
if sibling.get_text() == Stop_Point:
break
if sibling.name == 'div':
for children in sibling.children:
if isinstance(children, bs4.element.Tag):
if children.get_text() == Stop_Point:
break
print(children.get_text())

最新更新