-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheapestprice.py
88 lines (72 loc) · 3.03 KB
/
cheapestprice.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from urllib2 import urlopen as uReq
from bs4 import BeautifulSoup as soup
from Product import Product
"""
Take the kijiji website url and find the cheapest priced item on that page.
"""
def cheapest_price(my_url, givenproduct):
'''(url, string) -> (string, float, string, string)
This function takes a kijiji page url that has a list of products
and finds the cheapest product on that page.
REQ: The url must be a kijiji page url or else the program is unlikely
to work.
>>> cheapest_price("https://www.kijiji.ca/b-ontario/gtx-1070/k0l9004")
'''
# open up the connection, grab the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# do the html parsing
page_soup = soup(page_html, "html.parser")
# get all the regular advertisement containers
containers = page_soup.findAll('div', {'class': 'search-item regular-ad'})
# create empty dicts of prices, urls and dates attributres
product_to_prices = {}
product_to_urls = {}
product_to_dates = {}
# loop through the containers
for container in containers:
# find the name of the model of the product
product = container.a.text.strip()
if givenproduct.lower() in product.lower():
# extract the price and store it
# this used to be findAll
price = container.find('div',
{"class": "price"}).text
# get the url of the product
url = "https://www.kijiji.ca" + str(container.get('data-vip-url'))
date = container.find('span', {"class": "date-posted"}).text
# see if the price is valid
try:
# strip out the $ and ,
price = float(
price.replace('$', '').replace(',', '').replace(' ', ''))
# change from unicode to string
date = str(date)
product = str(product)
# map product's price, url and date
product_to_prices[product] = price
product_to_urls[product] = url
product_to_dates[product] = date
# otherwise skip it
except:
pass
# find the cheapest price out of the recorded prices
cheapest_price = min(product_to_prices.values())
# now check the cheapest product
for product in product_to_prices:
# extract the price from price dict
price = product_to_prices[product]
# check if the value of the products is < the previous lowest
if (price == cheapest_price):
# change the cheapest product to this one
cheapest_product = product
cheapest_url = product_to_urls[product]
cheapest_date = product_to_dates[product]
# make the product object and return it
return_product = Product()
return_product.set_date(cheapest_date)
return_product.set_price(cheapest_price)
return_product.set_url(cheapest_url)
return_product.set_name(cheapest_product)
return return_product