Is it possible to extract two HTML div tags in one “soup.find_all” with beautifulSoup? The divs are repeatedly called “event odd”, “event even” and i want to loop through them all
webpage code:
JavaScript
x
10
10
1
<div class="event odd">
2
<div class="featured-image">
3
<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>
4
</div>
5
6
<div class="event even">..</div> == $0
7
<div class="event odd">..</div> == $0
8
<div class="event even">..</div> == $0
9
<div class="event odd">..</div> == $0
10
My code:
concerts = soup.find_all([‘div’, {‘class’: ‘event odd’}, {‘class’: ‘event even’}])
JavaScript
1
3
1
for concert in concerts:
2
name = concert.find('a').get('href')
3
Advertisement
Answer
You can use Bitto Bennichan’s suggestion of using {'class': 'event'}
.
Or, if you must specify two values at once, you can pass them in a list:
JavaScript
1
3
1
>>> len(soup.find_all('div', {'class': ['event odd', 'event even']}))
2
5
3