|
| 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