I would like to scrape the title from an ecommerce site but I do not want to use the
.get_attribute()
Can anyone knows if is there a way to extract the title without use this function ?
import pandas as pd
from selenium.webdriver.common.by import By
website = 'https://www.skroutz.gr/c/40/kinhta-thlefwna.html?from=families'
title_list=[]
price_list=[]
description_list = []
rating_list = []
rating_star_list = []
for page in range(1,3):
website = 'https://www.skroutz.gr/c/40/kinhta-thlefwna.html?from=families' str(page)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(website)
titles = driver.find_elements("class name",'js-sku-link')
for title in titles:
title_list.append(title.get_attribute('title'))
Thank you.
CodePudding user response:
CSS_SELECTOR,'.card-content h2 > a'
will select titles without calling .get_attribute()
method but you have to invoke .text
Full working code:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())
title_list=[]
for page in range(1,3):
website = 'https://www.skroutz.gr/c/40/kinhta-thlefwna.html?from=families' str(page)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(website)
time.sleep(2)
titles = driver.find_elements(By.CSS_SELECTOR,'.card-content h2 > a')
for title in titles:
title_list.append(title.text)
print(title_list)
Output:
['Sony Xperia 1 III 5G Single SIM (12GB/256GB) Frosted Black', 'Sony Xperia 5 III 5G Dual SIM
(8GB/128GB) Μαύρο', 'Sony Xperia 10 III 5G Dual SIM (6GB/128GB) White', 'Sony Xperia 1 IV 5G Dual SIM (12GB/256GB) Μωβ', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Awesome Black', 'Realme 8i Dual SIM (4GB/128GB) Space Black', 'Apple iPhone 11 (4GB/64GB) Black', 'Samsung Galaxy A53 5G Dual SIM (6GB/128GB) Awesome Black', 'Realme GT Neo 2 5G Dual SIM (8GB/128GB) Neo Black', 'Realme 8i Dual SIM (4GB/64GB) Space Black', 'Apple iPhone 13 Pro Max 5G (6GB/128GB) Graphite', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Awesome White', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Awesome Mint', 'Xiaomi Redmi Note 11 Dual SIM (4GB/128GB) Graphite Gray', 'Apple iPhone 13 5G (4GB/128GB) Midnight', 'Realme 8i Dual SIM (4GB/64GB) Stellar Purple', 'Realme
8i Dual SIM (4GB/128GB) Stellar Purple', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Violet', 'Samsung Galaxy A52s 5G Dual SIM (8GB/256GB) Awesome Black', 'Samsung Galaxy A22 5G Dual SIM
(4GB/64GB) Grey', 'Xiaomi Redmi Note 10 Pro NFC Dual SIM (6GB/128GB) Onyx Gray', 'Apple iPhone 13 Pro Max 5G (6GB/256GB) Sierra Blue', 'Apple iPhone 11 (4GB/64GB) White', 'Apple iPhone 13
Pro 5G (6GB/256GB) Sierra Blue', 'Xiaomi Redmi Note 11 NFC Dual SIM (4GB/64GB) Twilight Blue', 'Realme GT 5G Dual SIM (8GB/128GB) Dashing Blue', 'Apple iPhone 13 5G (4GB/128GB) Starlight', 'Apple iPhone 13 5G (4GB/128GB) Pink', 'Apple iPhone 13 Pro Max 5G (6GB/256GB) Graphite', 'Apple iPhone 13 Pro 5G (6GB/128GB) Graphite', 'Xiaomi Redmi Note 11 Dual SIM (4GB/64GB) Twilight Blue', 'Xiaomi Redmi Note 11 NFC Dual SIM (4GB/64GB) Graphite Gray', 'Apple iPhone 13 Pro 5G
(6GB/128GB) Sierra Blue', 'Realme GT Master Edition 5G Dual SIM (6GB/128GB) Cosmos Black', 'Huawei Nova 9 Dual SIM (8GB/128GB) Black', 'Samsung Galaxy A52s 5G Dual SIM (8GB/256GB) Awesome
White', 'Apple iPhone 12 5G (4GB/64GB) Λευκό', 'CAT S42 H Dual SIM (3GB/32GB) Ανθεκτικό Smartphone Black', 'Apple iPhone 12 5G (4GB/64GB) Μαύρο', 'Apple iPhone 13 Pro 5G (6GB/256GB) Graphite', 'Apple iPhone 12 5G (4GB/128GB) Μαύρο', 'Apple iPhone 12 5G (4GB/128GB) Μπλε', 'Apple iPhone 13 Pro Max 5G (6GB/128GB) Sierra Blue', 'Realme 8 Dual SIM (4GB/64GB) Punk Black', 'Xiaomi Poco F3 5G Dual SIM (6GB/128GB) Night Black', 'Samsung Galaxy Z Fold 3 5G Dual SIM (12GB/256GB) Phantom Black', 'Realme GT Neo 2 5G Dual SIM (12GB/256GB) Neo Black', 'Xiaomi Redmi 10 Dual SIM (4GB/128GB) Carbon Gray', 'Sony Xperia 1 III 5G Single SIM (12GB/256GB) Frosted Black',
'Sony Xperia 5 III 5G Dual SIM (8GB/128GB) Μαύρο', 'Sony Xperia 10 III 5G Dual SIM (6GB/128GB) White', 'Sony Xperia 1 IV 5G Dual SIM (12GB/256GB) Μωβ', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Awesome Black', 'Realme 8i Dual SIM (4GB/128GB) Space Black', 'Apple iPhone 11 (4GB/64GB) Black', 'Samsung Galaxy A53 5G Dual SIM (6GB/128GB) Awesome Black', 'Realme GT Neo 2 5G Dual SIM (8GB/128GB) Neo Black', 'Realme 8i Dual SIM (4GB/64GB) Space Black', 'Apple iPhone 13
Pro Max 5G (6GB/128GB) Graphite', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Awesome White', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Awesome Mint', 'Xiaomi Redmi Note 11 Dual SIM (4GB/128GB) Graphite Gray', 'Apple iPhone 13 5G (4GB/128GB) Midnight', 'Realme 8i Dual SIM (4GB/64GB) Stellar Purple', 'Realme 8i Dual SIM (4GB/128GB) Stellar Purple', 'Samsung Galaxy A52s 5G Dual SIM (6GB/128GB) Violet', 'Samsung Galaxy A52s 5G Dual SIM (8GB/256GB) Awesome Black', 'Samsung Galaxy A22 5G Dual SIM (4GB/64GB) Grey', 'Xiaomi Redmi Note 10 Pro NFC Dual SIM (6GB/128GB) Onyx Gray', 'Apple iPhone 13 Pro Max 5G (6GB/256GB) Sierra Blue', 'Apple iPhone 11 (4GB/64GB) White', 'Apple iPhone 13 Pro 5G (6GB/256GB) Sierra Blue', 'Xiaomi Redmi Note 11 NFC Dual SIM (4GB/64GB) Twilight Blue', 'Realme GT 5G Dual SIM (8GB/128GB) Dashing Blue', 'Apple iPhone 13 5G (4GB/128GB) Starlight', 'Apple iPhone 13 5G (4GB/128GB) Pink', 'Apple iPhone 13 Pro Max 5G (6GB/256GB) Graphite', 'Apple iPhone 13 Pro 5G (6GB/128GB) Graphite', 'Xiaomi Redmi Note
11 Dual SIM (4GB/64GB) Twilight Blue', 'Xiaomi Redmi Note 11 NFC Dual SIM (4GB/64GB) Graphite
Gray', 'Apple iPhone 13 Pro 5G (6GB/128GB) Sierra Blue', 'Realme GT Master Edition 5G Dual SIM (6GB/128GB) Cosmos Black', 'Huawei Nova 9 Dual SIM (8GB/128GB) Black', 'Samsung Galaxy A52s 5G Dual SIM (8GB/256GB) Awesome White', 'Apple iPhone 12 5G (4GB/64GB) Λευκό', 'CAT S42 H Dual SIM (3GB/32GB) Ανθεκτικό Smartphone Black', 'Apple iPhone 12 5G (4GB/64GB) Μαύρο', 'Apple iPhone 13 Pro 5G (6GB/256GB) Graphite', 'Apple iPhone 12 5G (4GB/128GB) Μαύρο', 'Apple iPhone 12
5G (4GB/128GB) Μπλε', 'Apple iPhone 13 Pro Max 5G (6GB/128GB) Sierra Blue', 'Realme 8 Dual SIM (4GB/64GB) Punk Black', 'Xiaomi Poco F3 5G Dual SIM (6GB/128GB) Night Black', 'Samsung Galaxy Z Fold 3 5G Dual SIM (12GB/256GB) Phantom Black', 'Realme GT Neo 2 5G Dual SIM (12GB/256GB) Neo Black', 'Xiaomi Redmi 10 Dual SIM (4GB/128GB) Carbon Gray']