在单页 BS4 上获取多个价格
Taking multiple prices on single page 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 的方法可以在没有 forlooping 的情况下获得所有这些?
这将提供所有价格的平均值,
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.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 的方法可以在没有 forlooping 的情况下获得所有这些?
这将提供所有价格的平均值,
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