'Return a list of attribute values using Beautiful Soup

I'm doing a small project to scrape some data of a specific attribute, from this HTML:

  <players>
    <player username="Vac19" userid="2097691" name="Victor Carmona" startposition="" color="" score="9" new="0" rating="0" win="0"/>
    <player username="" userid="0" name="Crystal Carmona" startposition="" color="" score="10" new="0" rating="0" win="1"/>
    <player username="" userid="0" name="Cj" startposition="" color="" score="8" new="0" rating="0" win="0"/>
  </players>

End

How do I extract the values from the Score attribute using Beautiful Soup such as I get a list Score = [9, 10, 8]?



Solution 1:[1]

You can use a list comprehension:

scores = [tag['score'] for tag in soup.find_all('player')]

print(scores)

Output:

['9', '10', '8']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MendelG