skip to Main Content
def get_data(url):
  r = requests.get(url)
  soup = BeautifulSoup(r.text, 'html.parser')
  return soup

current_data = get_data(link)
x = current_data.find_all(text="Style Code:")

I’m trying to get the style code of a shoe off ebay but the problem is that it doesn’t have a specific class or any kind of unique identifier so I can’t just use find() to get the data. Currently I searched by text to find ‘Style Code:’ but how can I get to the next div? An example of a shoe product page would be this.

2

Answers


  1. Try this,

    spans = soup.find_all('span', attrs={'class':'ux-textspans'})
    style_code = None
    for idx, span in enumerate(spans):
        if span.text == 'Style Code:':
            style_code = spans[idx+1].text
            break
    print(style_code)
    # 554724-371
    

    Since there are lot’s of span is similar (with class 'ux-textspans') you need to iterate through it and find the next span after 'Style Code:'

    Login or Signup to reply.
  2. soup.select_one('span.ux-textspans:-soup-contains("Style Code:")').find_next('span').get_text(strip=True)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search