Issue
I have the html code below
<div class = "matches">
<div class = "conf">
Brazil vs. Colombia
</ div>
<div class = "targetHour"> 08:00 pm </ div>
</ div>
</ div>
<div class = "matches">
<div class = "conf">
Chilex Argentina
</ div>
<div class = "targetHour"> 08:00 pm </ div>
</ div>
</ div>
I need to get the value of the parent div and the value of the child div, without duplicating the result. Tie the schedule of each game with the respective parent.
This my python code
for nc in soup.find_all('div', attrs={'class': 'league-data'}):
campeonato = nc.text
for hr in soup.find('div', attrs={'class': 'match row cf'}).findAll("div",recursive=False):
print(campeonato + "|" + hr.text)
Solution
You can use zip()
function to tie matches to corresponding schedule:
from bs4 import BeautifulSoup
data = '''<div class = "conf">
Brazil vs. Colombia
</div>
<div class = "targetHour"> 08:00 pm </div>
</div>
</div>
<div class = "matches">
<div class = "conf">
Chilex Argentina
</div>
<div class = "targetHour"> 08:00 pm </div>
</div>
</div>'''
soup = BeautifulSoup(data, 'lxml')
for match, hour in zip( soup.select('div.conf'), soup.select('div.targetHour') ):
print(match.text.strip(), hour.text.strip())
Prints:
Brazil vs. Colombia 08:00 pm
Chilex Argentina 08:00 pm
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.