Spaces:
Sleeping
Sleeping
| import requests | |
| from bs4 import BeautifulSoup | |
| import pandas as pd | |
| def scrape_flipkart_products(search_query, filters, brands): | |
| gender = filters.get('gender', '').lower() | |
| url = "https://www.flipkart.com/search?q=" | |
| if search_query: | |
| url += f"{search_query.replace(' ', '+')}" | |
| # if brands: | |
| # for brand in brands: | |
| # brand=brand.split(" ") | |
| # brand="%2B".join(brand) | |
| # url += f"&p%5B%5D=facets.brand%255B%255D%3D{brand}" | |
| if gender == 'Male': | |
| url += "&p[]=facets.ideal_for%255B%255D%3DMen" | |
| elif gender == 'Female': | |
| url += "&p[]=facets.ideal_for%255B%255D%3DWomen" | |
| print(url) | |
| response = requests.get(url) | |
| soup = BeautifulSoup(response.content, 'html.parser') | |
| products = [] | |
| for product in soup.find_all('div', class_='_1xHGtK'): | |
| brand_element = product.find('div', class_='_2WkVRV') | |
| brand = brand_element.text if brand_element else "N/A" | |
| link_element = product.find('a', class_='_2UzuFa') | |
| link = "https://www.flipkart.com" + link_element['href'] if link_element else "N/A" | |
| image_element = product.find('img') | |
| image = image_element['src'] if image_element else "N/A" | |
| description_element = product.find('a', class_='IRpwTa') | |
| description = description_element.text if description_element else "N/A" | |
| price_element = product.find('div', class_='_30jeq3') | |
| price = price_element.text.replace('₹', '').replace(',', '') if price_element else "N/A" | |
| delivery_date_element = product.find('span', class_="_1TPvTK") | |
| delivery_date = delivery_date_element.text if delivery_date_element else "N/A" | |
| products.append({ | |
| 'Brand': brand, | |
| 'Link': link, | |
| 'Image': image, | |
| 'Description': description, | |
| 'Price': price, | |
| 'Delivery_Date': delivery_date | |
| }) | |
| return products | |