Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scraping hex codes
#2
If they have same class name will get all 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 Selector
Example 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'
Reply


Messages In This Thread
Scraping hex codes - by sophia_adams - Nov-30-2023, 04:12 PM
RE: Scraping hex codes - by snippsat - Nov-30-2023, 07:31 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020