forked from tuomaskivioja/3-automation-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebay_price_tracker.py
46 lines (36 loc) · 1.38 KB
/
ebay_price_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from bs4 import BeautifulSoup
import requests
import numpy as np
import csv
from datetime import datetime
LINK = "https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=p2380057.m570.l1313&_nkw=m1+macbook+air&_sacat=0"
def get_prices_by_link(link):
# get source
r = requests.get(link)
# parse source
page_parse = BeautifulSoup(r.text, 'html.parser')
# find all list items from search results
search_results = page_parse.find("ul",{"class":"srp-results"}).find_all("li",{"class":"s-item"})
item_prices = []
for result in search_results:
price_as_text = result.find("span",{"class":"s-item__price"}).text
if "to" in price_as_text:
continue
price = float(price_as_text[1:].replace(",",""))
item_prices.append(price)
return item_prices
def remove_outliers(prices, m=2):
data = np.array(prices)
return data[abs(data - np.mean(data)) < m * np.std(data)]
def get_average(prices):
return np.mean(prices)
def save_to_file(prices):
fields=[datetime.today().strftime("%B-%D-%Y"),np.around(get_average(prices),2)]
with open('prices.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(fields)
if __name__ == "__main__":
prices = get_prices_by_link(LINK)
prices_without_outliers = remove_outliers(prices)
print(get_average(prices_without_outliers))
save_to_file(prices)