[Python] Ebay Scraping to avoid listings that include "results matching fewer words"

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to scrape ebay sold listings on products that I have pulled from Lego to then work out average sold price of those listings.

    The problem I am having is that it is including products that fall under "Results Matching fewer words" and those are not relevant to what I want to see.

    I have tried to stop processing when it hits that selector but it then pulls through #N/As.

    Can you help edit this for use in python?

    def fetch_ebay_average_price(ebay_search_url):
    """ Fetch the average sold price and count of sold listings from eBay sold listings page. """
    try:
    service = Service(CHROME_DRIVER_PATH)
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.get(ebay_search_url)
    time.sleep(3)

    soup = BeautifulSoup(driver.page_source, 'html.parser')
    price_elements = soup.select('span.s-item__price')

    prices = []
    for price_element in price_elements:
    try:
    price_text = price_element.text.strip()
    price_value = float(re.sub(r'[£,$,€]', '', price_text.split()[0].replace(',', '')))
    prices.append(price_value)
    except Exception as e:
    print(f"Error processing price: {e}")
    continue

    driver.quit()
    if prices:
    average_price = round(statistics.mean(prices), 2)
    sold_count = len(prices)
    return f"£{average_price}", sold_count
    else:
    return "N/A", 0
    except Exception as e:
    print(f"Error fetching eBay average price: {e}")
    return "N/A", 0

    Continue reading...

Compartilhe esta Página