0

Hi i want to be able to grab the prices from this site, parse them into ints and then average them. Tried a few ways but keep struggling to parse out the final numbers.

import requests
from bs4 import BeautifulSoup

URL = 'https://www.watchfinder.co.uk/search?q=114060&orderby=AgeNewToOld'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}

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

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


prices = soup.find_all(class_=('prods_price'))
for price in prices:
    price = price.text
    print(price)

This gives me:

£9,450



£8,750



£8,450

Is there a way to average them? Sorry guys, thanks!

Matthew Casey
  • 145
  • 1
  • 3
  • 12
  • 1
    `...struggling to parse out the final numbers` - do you mean that you can't extract the data you need from the `soup`? All you are doing is printing the `price`, shouldn't you convert it to a float, put it in a container, sum the values in the container then divide? – wwii Nov 12 '19 at 23:18
  • 1
    You should provide a minimal example of `page.content` - [mcve]. – wwii Nov 12 '19 at 23:21
  • Im printing just to get a view of whats going on. im struggling to get to that point of converting – Matthew Casey Nov 12 '19 at 23:22
  • Another: [python: how to convert currency to decimal?](https://stackoverflow.com/questions/3887469/python-how-to-convert-currency-to-decimal) – wwii Nov 12 '19 at 23:34
  • I now have 9450 8750 8450 - How can i separate them? – Matthew Casey Nov 12 '19 at 23:34

2 Answers2

0

An example similar to what you'd have is below. You can do it leaner if you like, but this should give you an average of numbers in a list.

Edit: Replaced your Euro sign with dollar sign.

To convert a string to an int just use int(string)

price = ['$900','$100','$500','$50']


runSum = 0

for i in price:
    convt = i.replace('$', '') 
    numHld = int(convt)
    runSum = runSum + numHld

avg = runSum/len(price)

print(str(avg))
JR87
  • 95
  • 8
0

The following solution reads in your test list, and then uses the custom function convert_to_num to find the average

test = ['£9,450', "£8,750", "£8,450"]

def convert_to_num(my_list):
        total = 0
        count = 0
        for item in my_list:
                item = item.replace("£","").replace(",","")
                total += int(item)
                count += 1

        return total / count

new_avg = convert_to_num(test)
print(new_avg)

Returns 8883.333333333334

artemis
  • 6,857
  • 11
  • 46
  • 99
  • works but not in my forloop as it does it one at a time. is there a way to return all my separate loop outputs into a full list like your "test" list? – Matthew Casey Nov 12 '19 at 23:51