Untitled
from selenium import webdriver from selenium.webdriver.chrome.options import Options from bs4 import BeautifulSoup # Set up Chrome driver options chrome_options = Options() chrome_options.add_argument("--headless") # Run Chrome in headless mode # Set up Chrome driver driver = webdriver.Chrome(options=chrome_options) # Load the page url = 'https://www.amazon.com/s?k=laptops' driver.get(url) # Get the page source after JavaScript execution page_source = driver.page_source # Close the driver driver.quit() # Parse the page source with BeautifulSoup soup = BeautifulSoup(page_source, 'html.parser') # Scrape the desired information products = soup.find_all('div', {'data-component-type': 's-search-result'}) for product in products: title = product.find('span', {'class': 'a-size-medium'}).text.strip() price_element = product.find('span', {'class': 'a-offscreen'}) if price_element is not None: price = price_element.text.strip() else: price = 'N/A' rating = product.find('span', {'class': 'a-icon-alt'}).text.strip() print(f"Title: {title}") print(f"Price: {price}") print(f"Rating: {rating}") print()
Leave a Comment