0% found this document useful (0 votes)
19 views2 pages

Web Scraping

The document outlines a Python script that uses Selenium to scrape product titles and prices from an e-commerce site, specifically Amazon. It defines two classes, one for storing product information and another for handling the scraping process, including navigating pages and extracting data. The scraped data is then saved to a CSV file.

Uploaded by

islam2103076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Web Scraping

The document outlines a Python script that uses Selenium to scrape product titles and prices from an e-commerce site, specifically Amazon. It defines two classes, one for storing product information and another for handling the scraping process, including navigating pages and extracting data. The scraped data is then saved to a CSV file.

Uploaded by

islam2103076
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

WEBSCRAPING AN E-COMMERCE SITE

from selenium import webdriver


from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import csv

class one():
def __init__(self, title, price):
self.title = title
self.price = price

class two():

def data(self, name):


count = 1
page = 1
add_page = 10
maximum = 100

my_list = []

url = "https://www.amazon.com/s?k=" + name + "&page=" + str(page)

my_options = Options()
[email protected]
Q3AHNFKLYC my_options.headless = False
my_options.add_experimental_option("detach", True)
my_browser = webdriver.Chrome(ChromeDriverManager().install(),
options=my_options)
my_browser.maximize_window()
my_browser.get(url)
my_browser.set_page_load_timeout(12)

while True:
try:
if add_page * page > maximum:
break

if count > add_page :


count = 1
page = page + 1

title_path =
'//*[@id="search"]/div[1]/div[1]/div/span[3]/div[2]/div[' + str(count) +
']/div/span/div/div/div[2]/div[2]/div/div/div[1]/h2/a/span'
title = my_browser.find_element_by_xpath(title_path)
title_text = title.get_attribute("innerHTML").splitlines()[0]
title.click()

price_path = '//*[@id="priceblock_ourprice"'
price = my_browser.find_element_by_xpath(price_path)
price_text = price.get_attribute("innerHTML")

Proprietary
This file content.
is meant©Great
for Learning.
personal All Rights
use by Reserved. Unauthorized use or distribution prohibited
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.
url = "https://www.amazon.com/s?k=iphone+12"
my_browser.get(url)
my_browser.set_page_load_timeout(12)

my_info = one(title_text, price_text)


my_list.append(my_info)

count = count + 1

except Exception as e:
print("Excetion on the count number", count, e)
count = count + 1

if add_page * page > maximum:


break

if count > add_page :


count = 1
page = page + 1

url = "https://www.amazon.com/s?k=iphone+12"
my_browser.get(url)
my_browser.set_page_load_timeout(12)

my_browser.close()

return my_list
[email protected]
Q3AHNFKLYC fun_call = two()

with open('one.csv', 'w', newline='', encoding='utf-8') as csvfile:


data = csv.writer(csvfile, delimiter = ';', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
for article in fun_call.data("iphone 12"):
data.writerow([article.title, article.price])

Proprietary
This file content.
is meant©Great
for Learning.
personal All Rights
use by Reserved. Unauthorized use or distribution prohibited
[email protected] only.
Sharing or publishing the contents in part or full is liable for legal action.

You might also like