-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (109 loc) · 5.54 KB
/
main.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import requests
from bs4 import BeautifulSoup
from tabulate import tabulate
user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
def get_product_details_snapdeal(url, user_agent):
status_code = 503
while status_code == 503:
headers = {"User-Agent": user_agent}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
title_element = soup.find("h1", {"class": "pdp-e-i-head"})
title = title_element.get_text().strip() if title_element else "Title not found"
price_element = soup.find("span", {"class": "pdp-final-price"})
price = price_element.get_text().strip() if price_element else "Price not found"
image_element = soup.find("img", {"class": "cloudzoom"})
image = image_element["src"] if image_element else "Image not found"
description_element = soup.find("div", {"class": "col-xs-18"})
description = description_element.get_text().strip() if description_element else "Description not found"
status_code = 200
return {
"title": title,
"price": price,
"image": image,
"description": description
}
else:
print("Failed to retrieve Snapdeal product details. Retrying...")
def get_product_details_amazon(url, user_agent):
status_code = 503
while status_code == 503:
headers = {"User-Agent": user_agent}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
title_element = soup.find("span", {"id": "productTitle"})
title = title_element.get_text().strip() if title_element else "Title not found"
price_element = soup.find("span", {"class": "a-price-whole"})
price = price_element.get_text().strip() if price_element else "Price not found"
image_element = soup.find("img", {"id": "landingImage"})
image = image_element["src"] if image_element else "Image not found"
description_element = soup.find("div", {"id": "productDescription"})
description = description_element.get_text().strip() if description_element else "Description not found"
status_code = 200
return {
"title": title,
"price": price,
"image": image,
"description": description
}
else:
print("Failed to retrieve Amazon product details. Retrying...")
def get_product_details_flipkart(url, user_agent):
status_code = 503
while status_code == 503:
headers = {"User-Agent": user_agent}
response = requests.get(url, headers=headers)
if response.status_code == 200:
soup = BeautifulSoup(response.text, "html.parser")
title_element = soup.find("span", {"class": "_35KyD6"})
title = title_element.get_text().strip() if title_element else "Title not found"
price_element = soup.find("div", {"class": "_30jeq3 _16Jk6d"})
price = price_element.get_text().strip() if price_element else "Price not found"
image_element = soup.find("img", {"class": "_396cs4 _3exPp9"})
image = image_element["src"] if image_element else "Image not found"
description_element = soup.find("div", {"class": "_2FbOxw"})
description = description_element.get_text().strip() if description_element else "Description not found"
status_code = 200
return {
"title": title,
"price": price,
"image": image,
"description": description
}
else:
print("Failed to retrieve Flipkart product details. Retrying...")
def compare_prices(url_snapdeal, url_amazon, url_flipkart):
snapdeal_details = get_product_details_snapdeal(url_snapdeal, user_agent)
amazon_details = get_product_details_amazon(url_amazon, user_agent)
prices = {
"Snapdeal": snapdeal_details["price"],
"Amazon": amazon_details["price"],
}
try:
prices = {seller: float(price.replace("Rs.", "").replace(",", "")) for seller, price in prices.items()}
except ValueError:
prices = {seller: float("inf") for seller in prices}
cheapest_seller = min(prices, key=prices.get)
return {
cheapest_seller: prices[cheapest_seller],
"Product Details": {
"Title": snapdeal_details["title"],
"Image": snapdeal_details["image"],
"Description": snapdeal_details["description"]
}
}
if __name__ == "__main__":
snapdeal_url = input("Enter Snapdeal Product Link: ")
amazon_url = input("Enter Amazon Product Link: ")
flipkart_url = "Enter Flipkart Product Link: "
cheapest_product = compare_prices(snapdeal_url, amazon_url, flipkart_url)
if cheapest_product:
print(f"The cheapest seller is {list(cheapest_product.keys())[0]} with a price of {cheapest_product[list(cheapest_product.keys())[0]]}")
print("Product Details:")
print("Title:", cheapest_product["Product Details"]["Title"])
print("Image:", cheapest_product["Product Details"]["Image"])
print("Description:", cheapest_product["Product Details"]["Description"])
else:
print("Failed to retrieve product details or URLs are incorrect.")