在单个页面BS4上采用多个价格

我正在创建,以帮助我学习,但对我也很有用。我希望能够从(https://www.watchfinder.co.uk/search?q=114060&orderby=AgeNewToOld)一页解析多个价格,将它们转换为数字并取平均。该页面将更改,因此一天可能有3个价格,第二天可能有20个价格。我正在努力的部分是分开价格,以便我可以使用它们。 到目前为止,我有:

page = requests.get(URL,headers=headers)

soup = BeautifulSoup(page.content,'html.parser')


price = soup.find_all(class_=('prods_price'))
for price in price:
    price = price.text
    price = " ".join(price.split())
    price = price.split('£')
    price = [y.replace(',','') for y in price]
    price = list(map(int,price[1:]))
    print(price)

哪个给我

[9450]
[8750]
[8450]

谨记价格会发生变化,如何区分这些?还是BS4有一种方法可以在不进行循环的情况下获得所有这些信息?

uiuuihjh 回答:在单个页面BS4上采用多个价格

这将提供所有价格的平均值,

URL = 'https://www.watchfinder.co.uk/search?q=114060&orderby=AgeNewToOld'
page = requests.get(URL)
soup = BeautifulSoup(page.content,'html.parser')

prices = soup.find_all(class_=('prods_price'))
price_list  = [int((price.text).replace('£','').replace(',','')) for price in prices]
print(price_list)

def Average(lst): 
    return sum(lst) / len(lst)

print(Average(price_list))

输出:

  

[9250、8750、8450]

     

8816.666666666666

本文链接:https://www.f2er.com/3110368.html

大家都在问