-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (41 loc) · 1.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
from bs4 import BeautifulSoup
import streamlit as st
def extract_product_info(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
webpage = requests.get(url, headers=headers)
soup = BeautifulSoup(webpage.content, "html.parser")
# Extract product title
try:
title = soup.find("span", {"class": "VU-ZEz"}).text.strip()
except AttributeError:
title = None
# Extract price
try:
price = soup.find("div", {"class": "Nx9bqj CxhGGd"}).text.strip()
except AttributeError:
price = None
# Extract image URL
try:
image_div = soup.find("div", {"class": "z1kiw8"})
image_url = image_div.find("img")["src"] if image_div else None
except AttributeError:
image_url = None
# Extract Ingredients
try:
description = soup.find("td", string="Ingredients").find_next_sibling("td").text.strip()
ingredients_website = [ingredient.strip() for ingredient in description.split(",")]
except AttributeError:
ingredients_website = []
return title, price, image_url, ingredients_website
st.title("Flipkart Product Analyzer")
url = st.text_input("Enter the Flipkart product URL:")
if st.button("Show All Details"):
if url:
title, price, image_url, ingredients_website = extract_product_info(url)
if title and price and image_url:
st.subheader(title)
st.write(f"Price: {price}")
st.image(image_url)
else:
st.write("Unable to extract product information. Please check the URL and try again.")