MisterButter
MisterButter

Reputation: 797

How to select second div tag with same classname?

I'm trying to select the the second div tag with the info classname, but with no success using bs4 find_next. How Do you go about selecting the text inside the second div tag that share classname?

[<div class="info">
    <a href="/clubs/12/Manchester-United/overview">
        Manchester United<span class="playerClub badge-20 t1"></span>
    </a>
</div>
<div class="info">Defender</div>
<div class="info">
    <a href="/clubs/12/Manchester-United/overview">
        Manchester United<span class="playerClub badge-20 t1"></span>
    </a>
</div>
<div class="info">Defender</div>]

Here is what I have tried

from bs4 import BeautifulSoup
import requests

players_url =['http://www.premierleague.com//players/13559/Axel-Tuanzebe/stats']

# this is dict where we store all information:
players = {}

for url in players_url:
    player_page = requests.get(url)
    cont = soup(player_page.content, 'lxml')
    data = dict((k.contents[0].strip(), v.get_text(strip=True)) for k, v in zip(cont.select('.topStat span.stat, .normalStat span.stat'), cont.select('.topStat span.stat > span, .normalStat span.stat > span')))
    club = {"Club" : cont.find('div', attrs={'class' : 'info'}).get_text(strip=True)}
    position = {"Position": cont.find_next('div', attrs={'class' : 'info'})}
    players[cont.select_one('.playerDetails .name').get_text(strip=True)] = data

print(position)

Upvotes: 0

Views: 87

Answers (1)

IkarosKun
IkarosKun

Reputation: 463

You can try follows:

clud_ele = cont.find('div', attrs={'class' : 'info'})
club = {"Club" : clud_ele.get_text(strip=True)}
position = {"Position": clud_ele.find_next('div', attrs={'class' : 'info'})}

Upvotes: 1

Related Questions