Skip to content

Improved the handling of some corner cases #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions scrape_amazon/util/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ def extractPage(url: str) -> str:
reviewDate = productPage.findAll("span", {"class": "review-date"})
ratingsSpan = productPage.findAll("i", {"class": "review-rating"})
reviewTitlesSpan = productPage.findAll("a", {"class": "review-title-content"})
reviewTitlesSpan.extend( productPage.findAll("span", {"data-hook":"review-title", "class":"a-size-base review-title a-color-base review-title-content a-text-bold"}) ) # review from other country
reviewDescriptionSpan = productPage.findAll(
"span", {"class": "review-text-content"}
)

# Loop is initiated from 2 because we have to exclude the Top Positive and
# Top Critical Review which otherwise will get repeated.
for i in range(2, len(reviewrsSpan)):
# Check if Top critical review exist.
topReviewrsSpan = productPage.findAll("h4", {"class": "a-size-medium view-point-title"})
for i in range(len(topReviewrsSpan), len(reviewrsSpan)):
reviewers.append(reviewrsSpan[i].get_text())
ratings.append(int(ratingsSpan[i].get_text()[0]))
matches = datefinder.find_dates(reviewDate[i].get_text())
Expand All @@ -75,10 +78,11 @@ def extractPage(url: str) -> str:
def extractTotalPages(url):
r = get_URL(url)
productPage = BeautifulSoup(r.text, "html.parser")
# Check reviews instead of ratings
pageSpanText = productPage.findAll(
"span", {"class": "a-size-base a-color-secondary"}
"div", {"data-hook" : "cr-filter-info-review-rating-count", "class": "a-row a-spacing-base a-size-base"}
)[0].get_text()
totalReviews = int(re.findall(r"\d+", pageSpanText.replace(",", ""))[0])
totalReviews = int(re.findall(r"\d+", pageSpanText.strip().split("ratings")[1].replace(",", ""))[0])
return (
math.ceil(totalReviews / 10),
productPage.find("title").get_text(),
Expand All @@ -92,7 +96,8 @@ def scrape_reviews(url):
print(f"[scrape-amazon] Total Pages - {totalPages}")
print(f"[scrape-amazon] Total Reviews - {totalReviews}\n")
urlsToFetch = []
for page in range(1, totalPages + 1):
# Amazon only display reviews in the first 500 pages
for page in range(1, min(totalPages + 1, 500) ):
urlToFetch = url + f"?pageNumber={page}"
urlsToFetch.append(urlToFetch)

Expand Down
16 changes: 11 additions & 5 deletions scrape_amazon/util/urlFunctions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import time
from my_fake_useragent import UserAgent

url_prefix = "https://www.amazon"
Expand Down Expand Up @@ -27,10 +28,15 @@ def get_URL(url: str) -> str:
"""
user_agent = ua.random()
while True:
content: str = requests.get(url, headers={"User-Agent": user_agent})
if "[email protected]" in content.text:
user_agent = ua.random()
# Avoid ConnectionError
try:
content: str = requests.get(url, headers={"User-Agent": user_agent})
if "[email protected]" in content.text:
user_agent = ua.random()
continue
break
except:
time.sleep(1)
continue
break


return content