Home > Software design >  How can I extract a specific item attribute from an ebay listing using BeautifulSoup?
How can I extract a specific item attribute from an ebay listing using BeautifulSoup?

Time:11-06

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.

CodePudding user response:

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:'

CodePudding user response:

soup.select_one('span.ux-textspans:-soup-contains("Style Code:")').find_next('span').get_text(strip=True)
  • Related