Detect discount
This commit is contained in:
11
scrape.py
11
scrape.py
@@ -10,15 +10,20 @@ def fetch_product_info(urls):
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
soup = BeautifulSoup(response.text, 'html.parser')
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
product_name = soup.find('h1', class_='keyfacts__title').text.strip()
|
product_name = soup.find('h1', class_='keyfacts__title').text.strip()
|
||||||
product_price = soup.find('div', class_='m-price__price').text.strip()
|
current_price = soup.find('div', class_='m-price__price').text.strip()
|
||||||
product_info.append([product_name, product_price, url])
|
original_price_element = soup.find('span', class_='m-price__rrp')
|
||||||
|
original_price = original_price_element.text.strip() if original_price_element else "-"
|
||||||
|
discount_element = soup.find('div', class_='m-price__label')
|
||||||
|
discount = discount_element.text.strip() if discount_element else "-"
|
||||||
|
|
||||||
|
product_info.append([product_name, current_price, discount, original_price, url])
|
||||||
else:
|
else:
|
||||||
print(f"Failed to fetch URL: {url}")
|
print(f"Failed to fetch URL: {url}")
|
||||||
|
|
||||||
return product_info
|
return product_info
|
||||||
|
|
||||||
def print_product_table(product_info):
|
def print_product_table(product_info):
|
||||||
headers = ["Product Name", "Price", "URL"]
|
headers = ["Product Name", "Price", "Discount", "original price", "URL"]
|
||||||
print(tabulate(product_info, headers=headers, tablefmt="grid"))
|
print(tabulate(product_info, headers=headers, tablefmt="grid"))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user