BeautifulSoup4 prints data twice

Viewed 34

Function is called only once in whole code. When I added a simple in to be raised by +1. It counts as normal 1,2,3..

def is_in_highscores():
    world = "Monza"
    high_score_site = f"subtopic=highscores&world={world}&beprotection=-1&category={6}&profession=0&currentpage={6}"
    soup = high_score_suffix(high_score_site)
    high_score = soup.find_all(class_="DoNotBreak")
    for i in high_score:
        a = (i.findParent())
        b = [element.text for element in i.findParent().contents]
        print(b)

I guess it has something do to with double <td class="DoNotBreak".. This is what "a" is:

<tr style="background-color:#D4C0A1;"><td>300</td><td class="DoNotBreak"><a href="https://www.tibia.com/community/?subtopic=characters&amp;name=Faje">Faje</a></td><td class="DoNotBreak">Elder Druid</td><td>Monza</td><td style="text-align: right;">637</td><td style="text-align: right;">4,283,568,941</td></tr>

And this is what is printed as b.

['298', 'Loveable Wicked', 'Master Sorcerer', 'Monza', '638', '4,288,783,674']
['298', 'Loveable Wicked', 'Master Sorcerer', 'Monza', '638', '4,288,783,674']
['299', 'Sleepy Bzyku', 'Elite Knight', 'Monza', '637', '4,285,301,108']
['299', 'Sleepy Bzyku', 'Elite Knight', 'Monza', '637', '4,285,301,108']
['300', 'Faje', 'Elder Druid', 'Monza', '637', '4,283,568,941']
['300', 'Faje', 'Elder Druid', 'Monza', '637', '4,283,568,941']
1 Answers

Assuming, the HTML given in the question. Writing this answer

<tr style="background-color:#D4C0A1;">
    <td>300</td>
    <td class="DoNotBreak">
        <a href="https://www.tibia.com/community/?subtopic=characters&amp;name=Faje">Faje</a>
    </td>
    <td class="DoNotBreak">Elder Druid</td>
    <td>Monza</td>
    <td style="text-align: right;">637</td>
    <td style="text-align: right;">4,283,568,941</td>
</tr>

tr_ele = soup.find_all("tr")  //finds tr
for i in tr_ele:
    b = [element.text for element in i]
    print(b)

If finding a tr with className="DoNotBreak" is must then you can do this. I am trimming the extra values by giving only 0th index. Since I didn't find any other logic.

tr_ele = tr = soup.select("tr .DoNotBreak")[0].find_parent()
for i in tr_ele:
    print(i.text)
Related