Created
May 8, 2021 16:05
-
-
Save RoyTakanen/6501972772ace3a0d5f005da4b69b3f0 to your computer and use it in GitHub Desktop.
Crawl daily gold prices from tavex
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
#https://pastebin.fi/p/IRfb7PacOA5B/ | |
import requests | |
from bs4 import BeautifulSoup | |
from datetime import date | |
import pandas as pd | |
import os.path | |
data_file = 'data.xlsx' | |
today = date.today() | |
URL = 'https://tavex.fi/kulta/kultalaatat/' | |
page = requests.get(URL) | |
soup = BeautifulSoup(page.content, 'html.parser') | |
gold_ingots = soup.find_all('div', class_='grid__col--xs-6 grid__col--sm-4') | |
print("Hinnat päivänä:", today, end="\n\n") | |
if not os.path.isfile(data_file): | |
df = pd.DataFrame(columns=['Gold bar', 'Buy price', 'Sell price', 'Date']) | |
else: | |
df = pd.read_excel(data_file) | |
for gold_ingot in gold_ingots: | |
name = gold_ingot.find('span', class_='product__title-inner') | |
tavex_buy_price = gold_ingot.find('div', class_='product__price product__price--buy').find('span', 'product__price-value h-price-flash') | |
tavex_sell_price = gold_ingot.find('div', class_='product__price').find('span', 'product__price-value h-price-flash js-product-price-from') | |
clean_name = name.text.replace('Varastossa', '').replace('\n', '').lstrip() | |
if 'Valcambi Suisse' in clean_name: | |
new_row = {'Gold bar':clean_name, 'Buy price':tavex_buy_price.text, 'Sell price':tavex_sell_price.text, 'Date':today} | |
df = df.append(new_row, ignore_index=True) | |
print(name.text.replace('Varastossa', '').replace('\n', '').lstrip()) | |
print('Tavex ostaa:', tavex_buy_price.text) | |
print('Tavex myy:', tavex_sell_price.text) | |
print('-------------------------------------') | |
df.to_excel(data_file) | |
print("JOB HAS BEEN DONE.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment