Download app.py from usserrr/VsKraper: direct link, hf CLI and curl.
- Browser
- Download file 3.96 kB
-
https://huggingface.co/spaces/usserrr/VsKraper/resolve/main/app.py
- Command line
-
hf download hf://spaces/usserrr/VsKraper/app.py
-
curl -L -o app.py https://huggingface.co/spaces/usserrr/VsKraper/resolve/main/app.py
3.96 kB
| import random | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import gradio as gr | |
| from gradio import components | |
| def get_product_info(url): | |
| with open('user-agents.txt', 'r') as file: | |
| user_agents = file.read().splitlines() | |
| selected_user_agent_index = random.randint(0, len(user_agents) - 1) | |
| selected_user_agent = user_agents[selected_user_agent_index] | |
| headers = { | |
| 'User-Agent': selected_user_agent, | |
| 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | |
| 'Accept-Language': 'en-US,en;q=0.5', | |
| 'Accept-Encoding': 'gzip, deflate, br', | |
| 'DNT': '1', | |
| 'Connection': 'keep-alive', | |
| 'Upgrade-Insecure-Requests': '1', | |
| 'TE': 'Trailers', | |
| } | |
| base_url = 'https://flipkart.com' | |
| response = requests.get(url, headers=headers) | |
| product_info = {} | |
| if response.status_code == 200: | |
| soup = BeautifulSoup(response.content, "html.parser") | |
| title_element = soup.find('span', {'class': 'B_NuCI'}) | |
| product_info['Title'] = title_element.get_text().strip() if title_element else "Title not found" | |
| price_element = soup.find('div', {'class': '_30jeq3'}) | |
| product_info['Price'] = price_element.get_text().strip() if price_element else "Price not found" | |
| rating_element = soup.find('div', {'class': '_3LWZlK'}) | |
| product_info['Rating'] = rating_element.text if rating_element else "Rating not found" | |
| num_reviews_element = soup.find('span', {'class': '_2_R_DZ'}) | |
| product_info['Number of Reviews'] = num_reviews_element.text if num_reviews_element else "Number of Reviews not found" | |
| description_element = soup.find('div', {'class': '_1mXcCf'}) | |
| product_info['Description'] = description_element.get_text().strip() if description_element else "Description not found" | |
| image_links = [] | |
| image_elements = soup.find_all('img', {'class': '_396cs4'}) | |
| for img in image_elements: | |
| if 'src' in img.attrs and img['src'].startswith("http"): | |
| image_links.append(img['src']) | |
| product_info['First Image'] = image_links[0] if image_links else "" # First image link | |
| elements = soup.find_all(class_="_3UAT2v _16PBlm")[:3] | |
| review_urls = [] | |
| for element in elements: | |
| parent_a = element.find_parent('a') | |
| if parent_a and 'href' in parent_a.attrs: | |
| href = parent_a['href'] | |
| review_urls.append(base_url + href) | |
| product_info['Review URLs'] = review_urls | |
| else: | |
| product_info = { | |
| "Error": "Failed to fetch the page. Status code: " + str(response.status_code), | |
| "Title": "", | |
| "Price": "", | |
| "Rating": "", | |
| "Number of Reviews": "", | |
| "Description": "", | |
| "First Image": "", | |
| "Review URLs": [] | |
| } | |
| return ( | |
| product_info['First Image'], | |
| product_info['Title'], | |
| product_info['Price'], | |
| product_info['Rating'], | |
| product_info['Number of Reviews'], | |
| product_info['Description'], | |
| product_info['Review URLs'][0] if product_info['Review URLs'] else "" | |
| ) | |
| def create_gradio_interface(): | |
| iface = gr.Interface( | |
| fn=get_product_info, | |
| inputs=components.Textbox(label="Enter URL of the Flipkart product"), | |
| outputs=[ | |
| components.Image(type="pil", label="First Image"), | |
| components.Textbox(label="VsKraper"), | |
| components.Textbox(label="Product Price"), | |
| components.Textbox(label="Product Rating"), | |
| components.Textbox(label="Number of Reviews"), | |
| components.Textbox(label="Product Description"), | |
| components.Textbox(label="Review URL") | |
| ], | |
| title="VsKraper" # Set the title of the interface to "VsKraper" | |
| ) | |
| iface.launch() | |
| # To run the Gradio interface | |
| create_gradio_interface() | |