Created
March 27, 2017 21:35
-
-
Save thetrav/664c4847f9463e17bc81aaa636a496e3 to your computer and use it in GitHub Desktop.
Average daily price of limes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
def coles_price(): | |
r = requests.get('http://shop.coles.com.au/online/mobile/national/limes-loose') | |
if r.status_code == 200: | |
soup = BeautifulSoup(r.text, 'html.parser') | |
price_text = soup.find('p', {'class': 'price'}).get_text() | |
return float(price_text.replace('$', '')) | |
else: | |
raise Exception("failed to load coles price: {}, {}".format(r.status_code, r.text)) | |
def woolies_price(): | |
r = requests.get('https://www.woolworths.com.au/apis/ui/Search/products?AisleName=fruit-veg&Category=fresh-fruit&IsSpecial=false&PageNumber=1&PageSize=36&SearchTerm=limes&SortType=CUPAsc') | |
if r.status_code == 200: | |
products = r.json()['Products'] | |
fresh_lime = [p['Products'][0] for p in products if p['Name'].strip() == "Fresh Lime"] | |
if len(fresh_lime) > 0: | |
return float(fresh_lime[0]['Price']) | |
else: | |
raise Exception("Could not find fresh lime in {}".format(products)) | |
else: | |
raise Exception("failed to load woolies price: {}, {}".format(r.status_code, r.text)) | |
def avg_price(): | |
return (coles_price() + woolies_price()) / 2 | |
def main(): | |
print "avg price: {}".format(avg_price()) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment