I’m creating an to help me learn but is also useful to me. I want to be able to parse multiple prices from (https://www.watchfinder.co.uk/search?q=114060&orderby=AgeNewToOld) one page, convert them to numbers and average them. The page will change so it could have 3 prices one day and 20 the next. The part i am struggling with is separating the prices so that i can use them. So far i have:
JavaScript
x
14
14
1
page = requests.get(URL, headers=headers)
2
3
soup = BeautifulSoup(page.content, 'html.parser')
4
5
6
price = soup.find_all(class_=('prods_price'))
7
for price in price:
8
price = price.text
9
price = " ".join(price.split())
10
price = price.split('£')
11
price = [y.replace(',', '') for y in price]
12
price = list(map(int, price[1:]))
13
print(price)
14
Which gives me
JavaScript
1
4
1
[9450]
2
[8750]
3
[8450]
4
Baring in mind that the amount of prices can change, how can I separate these? Or is there a way with BS4 that can get all these without forlooping?
Advertisement
Answer
This will provide the average value for all prices,
JavaScript
1
13
13
1
URL = 'https://www.watchfinder.co.uk/search?q=114060&orderby=AgeNewToOld'
2
page = requests.get(URL)
3
soup = BeautifulSoup(page.content, 'html.parser')
4
5
prices = soup.find_all(class_=('prods_price'))
6
price_list = [int((price.text).replace('£', '').replace(',', '')) for price in prices]
7
print(price_list)
8
9
def Average(lst):
10
return sum(lst) / len(lst)
11
12
print(Average(price_list))
13
output:
[9250, 8750, 8450]
8816.666666666666