File size: 3,956 Bytes
740b369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import random
import requests
from bs4 import BeautifulSoup
import gradio as gr
from gradio import components

def get_product_info(url):
    with open('user-agents.txt', 'r') as file:
        user_agents = file.read().splitlines()

    selected_user_agent_index = random.randint(0, len(user_agents) - 1)
    selected_user_agent = user_agents[selected_user_agent_index]
    headers = {
        'User-Agent': selected_user_agent,
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.5',
        'Accept-Encoding': 'gzip, deflate, br',
        'DNT': '1',
        'Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'TE': 'Trailers',
    }

    base_url = 'https://flipkart.com'

    response = requests.get(url, headers=headers)
    product_info = {}

    if response.status_code == 200:
        soup = BeautifulSoup(response.content, "html.parser")

        title_element = soup.find('span', {'class': 'B_NuCI'})
        product_info['Title'] = title_element.get_text().strip() if title_element else "Title not found"

        price_element = soup.find('div', {'class': '_30jeq3'})
        product_info['Price'] = price_element.get_text().strip() if price_element else "Price not found"

        rating_element = soup.find('div', {'class': '_3LWZlK'})
        product_info['Rating'] = rating_element.text if rating_element else "Rating not found"

        num_reviews_element = soup.find('span', {'class': '_2_R_DZ'})
        product_info['Number of Reviews'] = num_reviews_element.text if num_reviews_element else "Number of Reviews not found"

        description_element = soup.find('div', {'class': '_1mXcCf'})
        product_info['Description'] = description_element.get_text().strip() if description_element else "Description not found"

        image_links = []
        image_elements = soup.find_all('img', {'class': '_396cs4'})
        for img in image_elements:
            if 'src' in img.attrs and img['src'].startswith("http"):
                image_links.append(img['src'])
        product_info['First Image'] = image_links[0] if image_links else ""  # First image link

        elements = soup.find_all(class_="_3UAT2v _16PBlm")[:3]
        review_urls = []
        for element in elements:
            parent_a = element.find_parent('a')
            if parent_a and 'href' in parent_a.attrs:
                href = parent_a['href']
                review_urls.append(base_url + href)
        product_info['Review URLs'] = review_urls
    else:
        product_info = {
            "Error": "Failed to fetch the page. Status code: " + str(response.status_code),
            "Title": "",
            "Price": "",
            "Rating": "",
            "Number of Reviews": "",
            "Description": "",
            "First Image": "",
            "Review URLs": []
        }

    return (
        product_info['First Image'],
        product_info['Title'],
        product_info['Price'],
        product_info['Rating'],
        product_info['Number of Reviews'],
        product_info['Description'],
        product_info['Review URLs'][0] if product_info['Review URLs'] else ""
    )

def create_gradio_interface():
    iface = gr.Interface(
        fn=get_product_info,
        inputs=components.Textbox(label="Enter URL of the Flipkart product"),
        outputs=[
            components.Image(type="pil", label="First Image"),
            components.Textbox(label="VsKraper"),
            components.Textbox(label="Product Price"),
            components.Textbox(label="Product Rating"),
            components.Textbox(label="Number of Reviews"),
            components.Textbox(label="Product Description"),
            components.Textbox(label="Review URL")
        ],
        title="VsKraper"  # Set the title of the interface to "VsKraper"
    )
    iface.launch()

# To run the Gradio interface
create_gradio_interface()