2
2
from bs4 import BeautifulSoup
3
3
4
4
# scraping amazon product page
5
+
6
+
5
7
class Product :
6
8
def __init__ (self , product_name : str ):
7
9
self .product_name = product_name
8
10
9
11
def get_product (self ):
10
-
12
+
11
13
try :
12
14
product_name = self .product_name
13
15
product_name = product_name .replace (" " , "+" )
@@ -19,7 +21,8 @@ def get_product(self):
19
21
r = requests .get (url , headers = headers )
20
22
soup = BeautifulSoup (r .content , "html.parser" )
21
23
product = soup .find ("div" , {"class" : "s-product-image-container" })
22
- product_link = product .find ("a" , {"class" : "a-link-normal" })["href" ]
24
+ product_link = product .find (
25
+ "a" , {"class" : "a-link-normal" })["href" ]
23
26
product_link = "https://www.amazon.in" + product_link
24
27
return {
25
28
"data" : product_link ,
@@ -57,8 +60,10 @@ def get_product_details(self):
57
60
}
58
61
r = requests .get (product_link , headers = headers )
59
62
soup = BeautifulSoup (r .content , "html.parser" )
60
- product_name = soup .find ("span" , {"id" : "productTitle" }).text .strip ()
61
- product_price = soup .find ("span" , {"class" : "a-price-whole" }).text .strip ()
63
+ product_name = soup .find (
64
+ "span" , {"id" : "productTitle" }).text .strip ()
65
+ product_price = soup .find (
66
+ "span" , {"class" : "a-price-whole" }).text .strip ()
62
67
product_rating = soup .find (
63
68
"span" , {"class" : "a-size-base a-color-base" }
64
69
).text .strip ()
@@ -165,7 +170,8 @@ def customer_review(self):
165
170
review_text = review_element .find (
166
171
"span" , {"data-hook" : "review-body" }
167
172
).text .strip ()
168
- review = [reviewer_name , rating , review_title , review_date , review_text ]
173
+ review = [reviewer_name , rating ,
174
+ review_title , review_date , review_text ]
169
175
return {
170
176
"data" : review ,
171
177
"message" : f"Product review has been fetched" ,
0 commit comments