我创造一个帮助我学习,但也是有用的我。我希望能够从(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有一种不用循环就能得到所有这些的方法吗?
发布于 2019-11-13 10:49:48
这将提供所有价格的平均值,
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://stackoverflow.com/questions/58836489
复制相似问题