Pretty simple scrape, however, I am having trouble printing the items. How do I print these items with their respective prices and images? Thanks a lot.
import time
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe', options=chrome_options)
products = []
def get_url(search_text):
template = 'https://www.amazon.com/s?k={}'
search_term = search_text.replace(' ', '+')
url = template.format(search_term)
url += '&pages={}'
for x in range(1, 3):
url_inc = url.format(x)
driver.get(url_inc)
time.sleep(3)
divs = driver.find_elements_by_tag_name('h2')
for div in divs:
products.append(div.text)
prices = driver.find_elements_by_class_name('a-price')
for price in prices:
products.append(price.text)
images = driver.find_elements_by_class_name('s-image')
for image in images:
products.append(image.get_attribute('src'))
return products
search = input("Enter your search :")
get_url(search)
print(products)
Pursuing Python Certification.