Skip to content

Commit 26cecea

Browse files
SparshRastogipre-commit-ci[bot]cclauss
authored
Create fetch_amazon_product_data.py (TheAlgorithms#7585)
* Create fetch_amazon_product_data.py This file provides a function which will take a product name as input from the user,and fetch the necessary information about that kind of products from Amazon like the product title,link to that product,price of the product,the ratings of the product and the discount available on the product in the form of a csv file,this will help the users by improving searchability and navigability and find the right product easily and in a short period of time, it will also be beneficial for performing better analysis on products * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update fetch_amazon_product_data.py Added type hints and modified files to pass precommit test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update fetch_amazon_product_data.py Added type hints and made changes to pass the precommit * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update fetch_amazon_product_data.py Modified function to return the data in the form of Pandas Dataframe,modified type hints and added a functionality to let the user determine if they need the data in a csv file * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update fetch_amazon_product_data.py Made some bug fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update and rename fetch_amazon_product_data.py to get_amazon_product_data.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update get_amazon_product_data.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 3a671b5 commit 26cecea

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
This file provides a function which will take a product name as input from the user,
3+
and fetch from Amazon information about products of this name or category. The product
4+
information will include title, URL, price, ratings, and the discount available.
5+
"""
6+
7+
8+
from itertools import zip_longest
9+
10+
import requests
11+
from bs4 import BeautifulSoup
12+
from pandas import DataFrame
13+
14+
15+
def get_amazon_product_data(product: str = "laptop") -> DataFrame:
16+
"""
17+
Take a product name or category as input and return product information from Amazon
18+
including title, URL, price, ratings, and the discount available.
19+
"""
20+
url = f"https://www.amazon.in/laptop/s?k={product}"
21+
header = {
22+
"User-Agent": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
23+
(KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""",
24+
"Accept-Language": "en-US, en;q=0.5",
25+
}
26+
soup = BeautifulSoup(requests.get(url, headers=header).text)
27+
# Initialize a Pandas dataframe with the column titles
28+
data_frame = DataFrame(
29+
columns=[
30+
"Product Title",
31+
"Product Link",
32+
"Current Price of the product",
33+
"Product Rating",
34+
"MRP of the product",
35+
"Discount",
36+
]
37+
)
38+
# Loop through each entry and store them in the dataframe
39+
for item, _ in zip_longest(
40+
soup.find_all(
41+
"div",
42+
attrs={"class": "s-result-item", "data-component-type": "s-search-result"},
43+
),
44+
soup.find_all("div", attrs={"class": "a-row a-size-base a-color-base"}),
45+
):
46+
try:
47+
product_title = item.h2.text
48+
product_link = "https://www.amazon.in/" + item.h2.a["href"]
49+
product_price = item.find("span", attrs={"class": "a-offscreen"}).text
50+
try:
51+
product_rating = item.find("span", attrs={"class": "a-icon-alt"}).text
52+
except AttributeError:
53+
product_rating = "Not available"
54+
try:
55+
product_mrp = (
56+
"₹"
57+
+ item.find(
58+
"span", attrs={"class": "a-price a-text-price"}
59+
).text.split("₹")[1]
60+
)
61+
except AttributeError:
62+
product_mrp = ""
63+
try:
64+
discount = float(
65+
(
66+
(
67+
float(product_mrp.strip("₹").replace(",", ""))
68+
- float(product_price.strip("₹").replace(",", ""))
69+
)
70+
/ float(product_mrp.strip("₹").replace(",", ""))
71+
)
72+
* 100
73+
)
74+
except ValueError:
75+
discount = float("nan")
76+
except AttributeError:
77+
pass
78+
data_frame.loc[len(data_frame.index)] = [
79+
product_title,
80+
product_link,
81+
product_price,
82+
product_rating,
83+
product_mrp,
84+
discount,
85+
]
86+
data_frame.loc[
87+
data_frame["Current Price of the product"] > data_frame["MRP of the product"],
88+
"MRP of the product",
89+
] = " "
90+
data_frame.loc[
91+
data_frame["Current Price of the product"] > data_frame["MRP of the product"],
92+
"Discount",
93+
] = " "
94+
data_frame.index += 1
95+
return data_frame
96+
97+
98+
if __name__ == "__main__":
99+
product = "headphones"
100+
get_amazon_product_data(product).to_csv(f"Amazon Product Data for {product}.csv")

0 commit comments

Comments
 (0)