Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathget_amazon_product_data.py
More file actions
Latest commit
112 lines (105 loc) · 3.63 KB
/
Copy pathget_amazon_product_data.py
File metadata and controls
112 lines (105 loc) · 3.63 KB
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
"""
This file provides a function which will take a product name as input from the user,
and fetch from Amazon information about products of this name or category. The product
information will include title, URL, price, ratings, and the discount available.
"""
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4",
# "httpx",
# "pandas",
# ]
# ///
fromitertoolsimportzip_longest
importhttpx
frombs4importBeautifulSoup
frompandasimportDataFrame
defget_amazon_product_data(product: str="laptop") ->DataFrame:
"""
Take a product name or category as input and return product information from Amazon
including title, URL, price, ratings, and the discount available.
"""
url=f"https://www.amazon.in/laptop/s?k={product}"
header= {
"User-Agent": (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
"(KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36"
),
"Accept-Language": "en-US, en;q=0.5",
}
soup=BeautifulSoup(
httpx.get(url, headers=header, timeout=10).text, features="lxml"
)
# Initialize a Pandas dataframe with the column titles
data_frame=DataFrame(
columns=[
"Product Title",
"Product Link",
"Current Price of the product",
"Product Rating",
"MRP of the product",
"Discount",
]
)
# Loop through each entry and store them in the dataframe
foritem, _inzip_longest(
soup.find_all(
"div",
attrs={"class": "s-result-item", "data-component-type": "s-search-result"},
),
soup.find_all("div", attrs={"class": "a-row a-size-base a-color-base"}),
):
try:
product_title=item.h2.text
product_link="https://www.amazon.in/"+item.h2.a["href"]
product_price=item.find("span", attrs={"class": "a-offscreen"}).text
try:
product_rating=item.find("span", attrs={"class": "a-icon-alt"}).text
exceptAttributeError:
product_rating="Not available"
try:
product_mrp= (
"₹"
+item.find(
"span", attrs={"class": "a-price a-text-price"}
).text.split("₹")[1]
)
exceptAttributeError:
product_mrp=""
try:
discount=float(
(
(
float(product_mrp.strip("₹").replace(",", ""))
-float(product_price.strip("₹").replace(",", ""))
)
/float(product_mrp.strip("₹").replace(",", ""))
)
*100
)
exceptValueError:
discount=float("nan")
exceptAttributeError:
continue
data_frame.loc[str(len(data_frame.index))] = [
product_title,
product_link,
product_price,
product_rating,
product_mrp,
discount,
]
data_frame.loc[
data_frame["Current Price of the product"] >data_frame["MRP of the product"],
"MRP of the product",
] =" "
data_frame.loc[
data_frame["Current Price of the product"] >data_frame["MRP of the product"],
"Discount",
] =" "
data_frame.index+=1
returndata_frame
if__name__=="__main__":
product="headphones"
get_amazon_product_data(product).to_csv(f"Amazon Product Data for {product}.csv")