webscrape / app.py
akhilpaul's picture
Update app.py
7571710 verified
Raw History Blame Contribute Delete
1.81 kB
import requests
from bs4 import BeautifulSoup
import gradio as gr
def get_product_info(product_url):
session = requests.Session()
session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-CA,en;q=0.9",
"Referer": "https://www.google.com"
})
# Set cookies for Sydney, NS store (Store #3101)
session.cookies.set("assortmentStoreId", "3101")
session.cookies.set("hasLocData", "1")
session.cookies.set("postalCode", "B1P7H9")
try:
response = session.get(product_url)
html = response.text
if "We like real shoppers, not robots" in html:
return "⚠️ Blocked by anti-bot measures. Try again later or use a different IP."
soup = BeautifulSoup(html, 'html.parser')
title_tag = soup.find('h1', {"itemprop": "name"})
price_tag = soup.find('span', {"itemprop": "price"})
product_name = title_tag.get_text(strip=True) if title_tag else "❌ Product name not found"
price_text = price_tag.get_text(strip=True) if price_tag else "❌ Price not found"
return f"🛍️ Product: {product_name}\n💵 Price: {price_text}"
except Exception as e:
return f"❌ Error: {str(e)}"
# Gradio UI
demo = gr.Interface(
fn=get_product_info,
inputs=gr.Textbox(label="Walmart.ca Product URL"),
outputs=gr.Textbox(label="Product Info"),
title="Walmart.ca Product Info Scraper (Sydney, NS Store)",
description="Paste a Walmart.ca product URL to get product name and price at the Sydney, NS store (Store #3101)."
)
demo.launch()