Skip to content

Commit

Permalink
Formatting and check.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhil25803 committed May 14, 2024
1 parent c57a064 commit 82ac30a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/scrape_up/amazon/products.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
from bs4 import BeautifulSoup


class Product:
"""Class for fetching and retrieving product details from Amazon."""

Expand All @@ -22,7 +23,7 @@ def fetch_product_page(self):
Returns:
BeautifulSoup: A BeautifulSoup object containing the parsed HTML content of the search page.
Raises:
Exception: If there is an error fetching the page.
"""
Expand Down Expand Up @@ -109,7 +110,9 @@ def get_product_image(self):
r = requests.get(product_link, headers=self.headers)
r.raise_for_status() # Raise HTTPError for bad responses
soup = BeautifulSoup(r.content, "html.parser")
product_image = soup.find("div", {"id": "imgTagWrapperId"}).find("img")["data-old-hires"]
product_image = soup.find("div", {"id": "imgTagWrapperId"}).find("img")[
"data-old-hires"
]
return {
"data": product_image,
"message": "Product image has been fetched",
Expand All @@ -135,11 +138,23 @@ def customer_review(self):
review_elements = soup.find_all("div", {"data-hook": "review"})
reviews = []
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()
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": reviewer_name,
"rating": rating,
Expand Down

0 comments on commit 82ac30a

Please sign in to comment.