In the code example below, I get different results for the print(len(block1)) item at the end of the code. I cannot seem to figure out what is causing this:
- my code,
- page loading with Selenium,
- some sort of anti-scrape method that Amazon uses, or
- a silly thing I am missing.
My ten most recent results were
LOG: 3/14/2020 - 2:30pm EDT
Length Results for 10 separate runs:
0 / 20 / 55 / 25 / 57 / 55 / 6 / 59 / 54 / 39
# python version: 3.8.1
#Import necessary modules
from selenium import webdriver # version 3.141.0
from bs4 import BeautifulSoup # version 4.8.2
#set computer path and object to chrome browser
chrome_path = r"C:\webdrivers\chromedriver.exe"
browser = webdriver.Chrome(chrome_path)
# search Amazon for "bar+soap"
# use 'get' for URL request and set object to variable "source"
browser.get("https://www.amazon.com/s?k=soap+bar&ref=nb_sb_noss_2")
source = browser.page_source
#use Beautiful Soup to parse html
page_soup = BeautifulSoup(source, 'html.parser')
#set a variable "block1" to find all "a" tags that fit criteria
block1 = page_soup.findAll("a", {"class":"a-size-base"})
#print the number of tags pulled
print(len(block1))