Untitled
unknown
plain_text
2 years ago
1.7 kB
5
Indexable
Never
from bs4 import BeautifulSoup import requests def getItemData(URL): try: session = requests.session() PRODUCT_PAGE = 'https://www.pricebefore.com/search/?q=' + URL session.headers.update( {'User-Agent': 'Mozilla/5.0 (Windows NT ' '10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, ' 'like Gecko) ' 'Chrome/90.0.4430.93 ' 'Safari/537.36', } ) productPageData = session.get(PRODUCT_PAGE) soup = BeautifulSoup(productPageData.content, 'html.parser') productName = soup.select_one("div.hd").find('h1').text try: priceData = soup.find('div', {'class': 'section'}).text.split() except: priceData = None currentPrice = float(priceData[2][1:].replace(',', '')) if priceData is not None else float( soup.find('span', {'class': 'price-final'}).text[4:].replace(',', '')) try: lowestPrice = float(priceData[5][1:].replace(',', '')) highestPrice = float(priceData[8][1:].replace(',', '')) except: lowestPrice = currentPrice highestPrice = currentPrice inStock = True # Stock data unavailable if len(productName) == 0 or (currentPrice == 0 and highestPrice == 0 and lowestPrice == 0): raise Exception('Error Fetching Data from the API') return {productName, currentPrice, lowestPrice, highestPrice, inStock} except: raise Exception('Error Fetching Data from the API') x = getItemData(input()) print(x)