If they have same class name will get all
Right click on selcet tag
Then in BS can use
Example here same class name,but can get specific tag with
find_all(class_='athenaProductVariations_colorSwatchInner')
Right click on selcet tag
Copy -> Copy selector
.Then in BS can use
select
or select_one
to get this tag only,this is called CSS SelectorExample here same class name,but can get specific tag with
span:nth-child
.from bs4 import BeautifulSoup html = '''\ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="icon" href="./favicon.ico" type="image/x-icon"> </head> <body> <span class="color_style" style="color:blue">blue</span> <span class="color_style" style="color:red">red</span> </body> </html>''' soup = BeautifulSoup(html, 'lxml')
>>> tag = soup.select_one('body > span:nth-child(1)') >>> tag <span class="color_style" style="color:blue">blue</span> >>> tag = soup.select_one('body > span:nth-child(2)') >>> tag <span class="color_style" style="color:red">red</span> >>> tag.get('style') 'color:red'