Spaces:
Sleeping
Sleeping
Update scraper.py
Browse files- scraper.py +106 -70
scraper.py
CHANGED
|
@@ -14,85 +14,125 @@ HEADERS = {
|
|
| 14 |
def fetch_soup(url):
|
| 15 |
resp = requests.get(url, headers=HEADERS, timeout=10)
|
| 16 |
resp.raise_for_status()
|
| 17 |
-
time.sleep(1) # Be polite and avoid rate
|
| 18 |
return BeautifulSoup(resp.text, "html.parser")
|
| 19 |
|
| 20 |
def scrape_amazon(query):
|
|
|
|
| 21 |
url = f"https://www.amazon.in/s?k={query}"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def scrape_1mg(query):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
})
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def scrape_netmeds(query):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def scrape_pharmeasy(query):
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def search_product(query):
|
| 89 |
-
amazon_products = scrape_amazon(query)
|
| 90 |
-
one_mg_products = scrape_1mg(query)
|
| 91 |
-
netmeds_products = scrape_netmeds(query)
|
| 92 |
-
pharmeasy_products = scrape_pharmeasy(query)
|
| 93 |
|
| 94 |
combined = []
|
| 95 |
-
max_len = max(len(amazon_products), len(one_mg_products),
|
|
|
|
| 96 |
|
| 97 |
for i in range(max_len):
|
| 98 |
combined.append({
|
|
@@ -115,10 +155,6 @@ def search_product(query):
|
|
| 115 |
"netmeds": netmeds_products[i] if i < len(netmeds_products) else {},
|
| 116 |
"pharmeasy": pharmeasy_products[i] if i < len(pharmeasy_products) else {},
|
| 117 |
})
|
|
|
|
| 118 |
return combined
|
| 119 |
|
| 120 |
-
# Example usage (uncomment to test):
|
| 121 |
-
# if __name__ == "__main__":
|
| 122 |
-
# products = search_product("paracetamol")
|
| 123 |
-
# for product in products:
|
| 124 |
-
# print(product)
|
|
|
|
| 14 |
def fetch_soup(url):
|
| 15 |
resp = requests.get(url, headers=HEADERS, timeout=10)
|
| 16 |
resp.raise_for_status()
|
| 17 |
+
time.sleep(1) # Be polite and avoid rate limits
|
| 18 |
return BeautifulSoup(resp.text, "html.parser")
|
| 19 |
|
| 20 |
def scrape_amazon(query):
|
| 21 |
+
print(f"Scraping Amazon for query: {query}")
|
| 22 |
url = f"https://www.amazon.in/s?k={query}"
|
| 23 |
+
try:
|
| 24 |
+
soup = fetch_soup(url)
|
| 25 |
+
products = []
|
| 26 |
+
for item in soup.select("div.s-result-item"):
|
| 27 |
+
name = item.select_one("span.a-text-normal")
|
| 28 |
+
name = name.get_text(strip=True) if name else "N/A"
|
| 29 |
+
price = item.select_one("span.a-price-whole")
|
| 30 |
+
price = price.get_text(strip=True) if price else "N/A"
|
| 31 |
+
img = item.select_one("img.s-image")
|
| 32 |
+
img = img["src"] if img else ""
|
| 33 |
+
link_el = item.select_one("a.a-link-normal.s-no-outline")
|
| 34 |
+
link = "https://www.amazon.in" + link_el["href"] if link_el else ""
|
| 35 |
+
if name != "N/A": # Only include if product name exists
|
| 36 |
+
products.append({
|
| 37 |
+
"name": name,
|
| 38 |
+
"price": price,
|
| 39 |
+
"img": img,
|
| 40 |
+
"link": link
|
| 41 |
+
})
|
| 42 |
+
return products
|
| 43 |
+
except requests.exceptions.RequestException as e:
|
| 44 |
+
print(f"Error scraping Amazon: {e}")
|
| 45 |
+
return []
|
| 46 |
|
| 47 |
def scrape_1mg(query):
|
| 48 |
+
print(f"Scraping 1mg for query: {query}")
|
| 49 |
+
url = "https://www.1mg.com/api/v5/search/results"
|
| 50 |
+
payload = {
|
| 51 |
+
"term": query,
|
| 52 |
+
"pageNumber": 1,
|
| 53 |
+
"pageSize": 20,
|
| 54 |
+
"type": "product"
|
| 55 |
+
}
|
| 56 |
+
try:
|
| 57 |
+
resp = requests.post(url, json=payload, headers=HEADERS)
|
| 58 |
+
resp.raise_for_status()
|
| 59 |
+
data = resp.json()
|
| 60 |
+
products = []
|
| 61 |
+
for item in data.get("data", {}).get("products", []):
|
| 62 |
+
name = item.get("name", "N/A")
|
| 63 |
+
price = item.get("mrp", "N/A")
|
| 64 |
+
img = item.get("image_url", "") # Adjust key based on actual response
|
| 65 |
+
link = item.get("product_url", "")
|
| 66 |
+
products.append({
|
| 67 |
+
"name": name,
|
| 68 |
+
"price": price,
|
| 69 |
+
"img": img,
|
| 70 |
+
"link": link
|
| 71 |
+
})
|
| 72 |
+
return products
|
| 73 |
+
except requests.exceptions.RequestException as e:
|
| 74 |
+
print(f"Error scraping 1mg: {e}")
|
| 75 |
+
return []
|
| 76 |
|
| 77 |
def scrape_netmeds(query):
|
| 78 |
+
print(f"Scraping Netmeds for query: {query}")
|
| 79 |
+
url = f"https://www.netmeds.com/mservice/rest/v2/product/search?term={query}&page=1&per_page=20"
|
| 80 |
+
try:
|
| 81 |
+
resp = requests.get(url, headers=HEADERS)
|
| 82 |
+
resp.raise_for_status()
|
| 83 |
+
data = resp.json()
|
| 84 |
+
products = []
|
| 85 |
+
for item in data.get("products", []):
|
| 86 |
+
name = item.get("name", "N/A")
|
| 87 |
+
price = item.get("price", "N/A")
|
| 88 |
+
img = item.get("image", "")
|
| 89 |
+
link = item.get("url", "")
|
| 90 |
+
products.append({
|
| 91 |
+
"name": name,
|
| 92 |
+
"price": price,
|
| 93 |
+
"img": img,
|
| 94 |
+
"link": link
|
| 95 |
+
})
|
| 96 |
+
return products
|
| 97 |
+
except requests.exceptions.RequestException as e:
|
| 98 |
+
print(f"Error scraping Netmeds: {e}")
|
| 99 |
+
return []
|
| 100 |
|
| 101 |
def scrape_pharmeasy(query):
|
| 102 |
+
print(f"Scraping PharmEasy for query: {query}")
|
| 103 |
+
url = f"https://pharmeasy.in/api/search/search?q={query}"
|
| 104 |
+
try:
|
| 105 |
+
resp = requests.get(url, headers=HEADERS)
|
| 106 |
+
resp.raise_for_status()
|
| 107 |
+
data = resp.json()
|
| 108 |
+
products = []
|
| 109 |
+
for item in data.get("data", {}).get("products", []):
|
| 110 |
+
name = item.get("name", "N/A")
|
| 111 |
+
price = item.get("sale_price", "N/A")
|
| 112 |
+
img = item.get("image", "")
|
| 113 |
+
link = item.get("slug", "")
|
| 114 |
+
if link:
|
| 115 |
+
link = f"https://pharmeasy.in/product/{link}"
|
| 116 |
+
products.append({
|
| 117 |
+
"name": name,
|
| 118 |
+
"price": price,
|
| 119 |
+
"img": img,
|
| 120 |
+
"link": link
|
| 121 |
+
})
|
| 122 |
+
return products
|
| 123 |
+
except requests.exceptions.RequestException as e:
|
| 124 |
+
print(f"Error scraping PharmEasy: {e}")
|
| 125 |
+
return []
|
| 126 |
|
| 127 |
def search_product(query):
|
| 128 |
+
amazon_products = [{"source": "Amazon", **p} for p in scrape_amazon(query)]
|
| 129 |
+
one_mg_products = [{"source": "1mg", **p} for p in scrape_1mg(query)]
|
| 130 |
+
netmeds_products = [{"source": "Netmeds", **p} for p in scrape_netmeds(query)]
|
| 131 |
+
pharmeasy_products = [{"source": "PharmEasy", **p} for p in scrape_pharmeasy(query)]
|
| 132 |
|
| 133 |
combined = []
|
| 134 |
+
max_len = max(len(amazon_products), len(one_mg_products),
|
| 135 |
+
len(netmeds_products), len(pharmeasy_products))
|
| 136 |
|
| 137 |
for i in range(max_len):
|
| 138 |
combined.append({
|
|
|
|
| 155 |
"netmeds": netmeds_products[i] if i < len(netmeds_products) else {},
|
| 156 |
"pharmeasy": pharmeasy_products[i] if i < len(pharmeasy_products) else {},
|
| 157 |
})
|
| 158 |
+
|
| 159 |
return combined
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|