gpt4 book ai didi

python - 如何在 beautifulsoup 的多个 div 中找到所需的数据

转载 作者:太空宇宙 更新时间:2023-11-04 05:35:08 25 4
gpt4 key购买 nike

这是html代码我正在尝试在多个 div 标签中选择数据

<div class="details-wrapper apps-secondary-color">
<div class="details-section metadata">
<div class="details-section-heading">
<div class="details-section-contents">
<div class="meta-info">
<div class="title">Updated</div>
<div class="content" itemprop="datePublished">March 7, 2016</div>
</div>
<div class="meta-info">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info contains-text-link">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info contains-text-link">
<div class="meta-info">
<div class="meta-info meta-info-wide">
<div class="details-sharing-section">
</div>
<div class="details-section-divider"></div>
</div>
</div>
</div>

我想选择2016年3月7日我如何在 beautifulsoup 中选择它

最佳答案

您可以使用 soup.find('div', {'itemprop': 'datePublished'})选择 div带有 itemprop 的元素datePublished .

演示

from bs4 import BeautifulSoup

content = '''<div class="details-wrapper apps-secondary-color">
<div class="details-section metadata">
<div class="details-section-heading">
<div class="details-section-contents">
<div class="meta-info">
<div class="title">Updated</div>
<div class="content" itemprop="datePublished">March 7, 2016</div>
</div>
<div class="meta-info">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info contains-text-link">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info">
<div class="meta-info contains-text-link">
<div class="meta-info">
<div class="meta-info meta-info-wide">
<div class="details-sharing-section">
</div>
<div class="details-section-divider"></div>
</div>
</div>
</div>'''

soup = BeautifulSoup(content)
date = soup.find('div', {'itemprop':'datePublished'})
print(date.text)

输出

March 7, 2016

关于python - 如何在 beautifulsoup 的多个 div 中找到所需的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35893289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com