File size: 1,941 Bytes
78489d3
 
 
 
 
 
 
 
 
ca05260
 
 
 
 
78489d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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