Spaces:
Runtime error
Runtime error
Download app.py from humeraxkhan/INDIAMART-SCRAPER: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/humeraxkhan/INDIAMART-SCRAPER/resolve/main/app.py
- Command line
-
hf download hf://spaces/humeraxkhan/INDIAMART-SCRAPER/app.py
-
curl -L -o app.py https://huggingface.co/spaces/humeraxkhan/INDIAMART-SCRAPER/resolve/main/app.py
2.67 kB
| import streamlit as st | |
| import pandas as pd | |
| from bs4 import BeautifulSoup | |
| import requests | |
| # Streamlit UI | |
| st.set_page_config(page_title="IndiaMART Scraper", layout="centered") | |
| st.title("π¦ IndiaMART Product Scraper") | |
| # Input fields | |
| query = st.text_input("π Enter Product Search Query", value="infrared lamp sensor") | |
| total_pages = st.number_input("π Number of Pages to Scrape", value=2, min_value=1) | |
| if st.button("π Start Scraping"): | |
| product_names, prices, cities, addresses, companies = [], [], [], [], [] | |
| for page in range(1, total_pages + 1): | |
| url = f"https://dir.indiamart.com/search.mp?ss={query.replace(' ', '+')}&page={page}" | |
| st.info(f"π Scraping Page {page}: {url}") | |
| headers = { | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" | |
| } | |
| try: | |
| res = requests.get(url, headers=headers, timeout=10) | |
| soup = BeautifulSoup(res.text, "html.parser") | |
| cards = soup.find_all("div", class_="card") | |
| if not cards: | |
| st.warning(f"β οΈ No product cards found on Page {page}") | |
| continue | |
| for card in cards: | |
| name = card.find("a", class_="cardlinks") | |
| price = card.find("p", class_="price") | |
| city_span = card.find("span", class_="elps elps1") | |
| address_p = card.find("p", class_="tac wpw") | |
| company = card.find("a", {"data-click": "^CompanyName"}) | |
| product_names.append(name.text.strip() if name else "No Name") | |
| prices.append(price.text.strip() if price else "N/A") | |
| cities.append(city_span.text.strip() if city_span else "N/A") | |
| addresses.append(address_p.text.strip() if address_p else "N/A") | |
| companies.append(company.text.strip() if company else "N/A") | |
| except Exception as e: | |
| st.error(f"β Failed to load page {page}: {e}") | |
| # Create DataFrame and Excel | |
| if product_names: | |
| df = pd.DataFrame({ | |
| "Product Name": product_names, | |
| "Price": prices, | |
| "City": cities, | |
| "Address": addresses, | |
| "Company": companies | |
| }) | |
| filename = f"{query.replace(' ', '_')}_indiamart_results.xlsx" | |
| df.to_excel(filename, index=False) | |
| st.success("β Scraping complete!") | |
| with open(filename, "rb") as f: | |
| st.download_button("π₯ Download Excel", f, file_name=filename) | |
| else: | |
| st.warning("β οΈ No data extracted. Try a different query or increase pages.") | |