You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ebay已售商品价格爬虫优化:过滤国际卖家及无关结果

eBay已售商品价格爬虫过滤无效条目问题

我用BeautifulSoup开发了爬取eBay已售商品价格的代码,整体运行效果良好,但存在一处问题:会抓取到搜索结果页里两类无效内容——国际卖家的商品,以及匹配关键词更少的结果。

我需要过滤掉这些无效条目,具体需求是识别包含特定子元素(例如带s-item__location类的span元素,仅国际卖家商品会包含该元素)的父级商品条目并排除,但尝试多种方法后均未成功。


问题解决(更新)

感谢@Driftr95的帮助,我已解决该问题,以下是可用代码:

import xlwings as xw
from bs4 import BeautifulSoup
import requests
import statistics

@xw.func
def get_prices(url,args =[]):
    url = requests.get(url).content
    soup = BeautifulSoup(url,'lxml')

    products = []
    rsecSel = 'li:not(.srp-river-answer--REWRITE_START ~ li)'
    iDetSel = f'div[id=\"srp-river-results\"] {rsecSel} div.s-item__details'
    results = soup.select(f'{iDetSel}:not(:has(span.s-item__location))')
    for item in results:
        price = item.find('span', class_='s-item__price').text.replace('$', '').replace(',', '')

        if 'to' not in price:
            price = float(price)
            products.append(price)
    
        mean = round(statistics.mean(products), 2)
        median = round(statistics.median(products), 2)

    return mean, median

现在我拥有了一个可在Excel中运行的函数,能够自动查询eBay已售商品价格,并且可以快速批量处理大量商品。


最初编写的代码

import xlwings as xw
import bs4 as bs
import requests
import statistics

@xw.func
def get_prices(url,args =[]):
    url_base = requests.get(url).text
    soup = bs.BeautifulSoup(url_base,'lxml')

    products = []
    results = soup.find('div', {'class': 'srp-river-results clearfix'}).find_all('div', {'class': ['s-item__details clearfix'] })
    for item in results:
        price = item.find('span', class_='s-item__price').text.replace('$', '').replace(',', '')

        if 'to' not in price:
            price = float(price)
            products.append(price)

#def calculate_averages(products):
    mean = round(statistics.mean(products), 2)
    median = round(statistics.median(products), 2)
    mode = round(statistics.mode(products), 2)

    return mean, median, mode

内容的提问来源于stack exchange,提问作者Jose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:35:19