【问题标题】:Extract two div tags in one BS find_all在一个 BS find_all 中提取两个 div 标签
【发布时间】:2019-10-17 19:56:41
【问题描述】:

是否可以使用 beautifulSoup 在一个“soup.find_all”中提取两个 HTML div 标签? div 被反复称为“奇数事件”、“偶数事件”,我想循环遍历它们

网页代码:

<div class="event odd">
    <div class="featured-image">
        <a href="https://dme-promotions.com/event/gloryhammer-beast-in-black-wind-rose/" style="background-image:url('https://dme-promotions.com/wp-content/uploads/2019/02/Gloryhammer-600x395.jpg');"></a>
    </div>

<div class="event even">..</div> == $0
<div class="event odd">..</div> == $0
<div class="event even">..</div> == $0
<div class="event odd">..</div> == $0

我的代码:

concerts = soup.find_all(['div', {'class': 'event odd'}, {'class': 'event even'}])

for concert in concerts:
    name = concert.find('a').get('href')

【问题讨论】:

  • 为什么不简单地获取所有带有“事件”类的 div?
  • @Bitto Bennichan 工作完美,谢谢。

标签: python html beautifulsoup


【解决方案1】:

您可以使用Bitto Bennichan's suggestion 或使用{'class': 'event'}

或者,如果您必须同时指定两个值,您可以将它们传递到一个列表中:

>>> len(soup.find_all('div', {'class': ['event odd', 'event even']}))
5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2023-01-04
    • 2013-03-18
    • 2021-09-28
    • 2014-10-31
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多