forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.py
180 lines (171 loc) · 5.96 KB
/
products.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import requests
from bs4 import BeautifulSoup
class Product:
def __init__(self, product_name: str):
self.product_name = product_name
def get_product(self):
try:
product_name = self.product_name
product_name = product_name.replace(" ", "+")
url = f"https://www.amazon.in/s?k={product_name}"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "html.parser")
product = soup.find("div", {"class": "s-product-image-container"})
product_link = product.find(
"a", {"class": "a-link-normal"})["href"]
product_link = "https://www.amazon.in" + product_link
return {
"data": product_link,
"message": f"Product data has been fetched",
}
except:
return {
"data": None,
"message": f"Unable to fetch product's data",
}
# Get product details
def get_product_details(self):
"""
Class - `Product`\n
Example -\n
```python
product = Product(product_name="watch")
product.get_product_details()
```
Return\n
```python
return
{
"data": product_details,
"message": f"Product detail has been fetched",
}
```
"""
try:
product_link = self.get_product()["data"]
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}
r = requests.get(product_link, headers=headers)
soup = BeautifulSoup(r.content, "html.parser")
product_name = soup.find(
"span", {"id": "productTitle"}).text.strip()
product_price = soup.find(
"span", {"class": "a-price-whole"}).text.strip()
product_rating = soup.find(
"span", {"class": "a-size-base a-color-base"}
).text.strip()
product_details = {
"product_name": product_name,
"product_price": product_price,
"product_rating": product_rating,
"product_link": product_link,
}
return {
"data": product_details,
"message": f"Product detail has been fetched",
}
except:
return {
"data": None,
"message": f"Unable to fetch product detail",
}
# Get product image
def get_product_image(self):
"""
Class - `Product`\n
Example -\n
```python
product = Product(product_name="watch")
product.get_product_image()
```
Return\n
```python
return
{
"data": product_image,
"message": f"Product image has been fetched",
}
```
"""
try:
product_link = self.get_product()["data"]
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}
r = requests.get(product_link, headers=headers)
soup = BeautifulSoup(r.content, "html.parser")
product_image = soup.find(
"img", {"class": "a-dynamic-image a-stretch-horizontal"}
)["src"]
return {
"data": product_image,
"message": f"Product image has been fetched",
}
except:
return {
"data": None,
"message": f"Unable to fetch product image",
}
# Get customer reviews
def customer_review(self):
"""
Class - `Product`\n
Example -\n
```python
product = Product(product_name="watch")
product.customer_review()
```
Return\n
```python
return
{
"data": review,
"message": f"Product review has been fetched",
}
```
"""
try:
product_link = self.get_product()["data"]
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
}
r = requests.get(product_link, headers=headers)
soup = BeautifulSoup(r.content, "html.parser")
review_elements = soup.find_all("div", {"data-hook": "review"})
for review_element in review_elements:
reviewer_name = review_element.find(
"span", {"class": "a-profile-name"}
).text
rating = (
review_element.find("i", {"class": "a-icon-star"})
.find("span", {"class": "a-icon-alt"})
.text
)
review_title = review_element.find(
"a", {"data-hook": "review-title"}
).text.strip()
review_date = review_element.find(
"span", {"data-hook": "review-date"}
).text
review_text = review_element.find(
"span", {"data-hook": "review-body"}
).text.strip()
review = [reviewer_name, rating,
review_title, review_date, review_text]
return {
"data": review,
"message": f"Product review has been fetched",
}
except:
return {
"data": None,
"message": f"Unable to fetch product review",
}