Crawl daily gold prices from tavex (python)

📅 2021-05-08T13:05:31.000Z
👁️ 204 katselukertaa
🔓 Julkinen


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.")