timer_function.py 1.21 KB
Newer Older
Arham Noman committed
1 2 3 4 5 6 7 8 9
import datetime
import logging

import azure.functions as func

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import pandas as pd

10
my_url = "YOUR EBAY URL"    # Change this to your search url
Arham Noman committed
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

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    #  Make the client object and read the html into a soup object
    uClient = uReq(my_url)
    page_html = uClient.read()
    uClient.close()

    page_soup = soup(page_html, 'html.parser')
    divs = page_soup.findAll('div',{'class':'s-item__details clearfix'})

    # Extract relevant data from the soup object

    prices = []

    for PS5 in divs: 
        price = PS5.find('span',{'class':"s-item__price"})
        price = price.text[1:]
        price = price.replace(',','')
        prices.append(price)

    # Clean data and save to a file for later use
    
36
    prices = pd.DataFrame(pd.to_numeric(prices,errors='coerce')).dropna()
Arham Noman committed
37 38 39 40 41
    name = "PS5" + str(utc_timestamp)  +".csv"
    name = "".join( x for x in name if (x.isalnum() or x in "._- "))
    prices.to_csv(name)

    logging.info('Python timer trigger function ran at %s', utc_timestamp)