簡體   English   中英

Python - 使用 Selenium 定位屬性

[英]Python - Locate attributes with Selenium

我想提取linkpicture link
來自此鏈接的sponsored flagrating stars https://www.skroutz.gr/c/40/kinhta-thlefwna.html 我設法提取其余部分,但對於那些我只能使用.get_attribute()函數提取它們,我無法使用它,因為我想將它們存儲在列表中。

那么任何人都可以幫助我如何找到這些屬性嗎?

提前致謝。

下面的示例將幫助您使用 Selenium 定位屬性

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()))

lst=[]

website = 'https://www.skroutz.gr/c/40/kinhta-thlefwna.html'
driver.get(website)
time.sleep(2)
cards = driver.find_elements(By.XPATH,'//*[@id="sku-list"]/li') 
for card in cards:
    link= card.find_element(By.XPATH,'.//*[@class="js-sku-link pic"]').get_attribute('href')
    img =  card.find_element(By.XPATH,'.//*[@class="js-sku-link pic"]/img').get_attribute('src')
    star_rating = card.find_element(By.XPATH,'.//*[@class="rating-wrapper"]/span').text
    lst.append({
        'link':link,
        'img':img,
        'star_rating':star_rating})
print(lst)
    

輸出:

[{'link': 'https://www.skroutz.gr/s/30475704/Sony-Xperia-1-III-5G-Single-SIM-12GB-256GB-Frosted-Black.html?from=featured&product_id=85046151', 'img': 'https://c.scdn.gr/images/sku_main_images/030475/30475704/large_20211214103218_sony_xperia_1_iii_5g_12gb_256gb_single_sim_frosted_black.jpeg', 'star_rating': '4.8'}, {'link': 'https://www.skroutz.gr/s/32430220/Sony-Xperia-5-III-5G-Dual-SIM-8GB-128GB-%CE%9C%CE%B1%CF%8D%CF%81%CE%BF.html?from=featured&product_id=95502499', 'img': 'https://a.scdn.gr/images/sku_main_images/032430/32430220/large_20211213172910_sony_xperia_5_iii_5g_8gb_128gb_mayro.jpeg', 'star_rating': '4.7'}, {'link': 'https://www.skroutz.gr/s/29156696/Sony-Xperia-10-III-5G-Dual-SIM-6GB-128GB-White.html?from=featured&product_id=77953206', 'img': 'https://b.scdn.gr/images/sku_main_images/029156/29156696/large_20211214102839_sony_xperia_10_iii_5g_6gb_128gb_white.jpeg', 'star_rating': '4.3'}, {'link': 'https://www.skroutz.gr/s/28946324/Sony-Xperia-10-III-5G-Dual-SIM-6GB-128GB-Black.html?from=featured&product_id=77947125', 'img': 'https://b.scdn.gr/images/sku_main_images/028946/28946324/large_20210518104628_sony_xperia_10_iii_128gb_black.jpeg', 'star_rating': '4.0'}, {'link': 'https://www.skroutz.gr/s/30662995/Samsung-Galaxy-A52s-5G-Dual-SIM-6GB-128GB-Awesome-Black.html', 'img': 'https://c.scdn.gr/images/sku_main_images/030662/30662995/large_20210830101603_samsung_galaxy_a52s_128gb_awesome_black.jpeg', 'star_rating': '4.4'}, {'link': 'https://www.skroutz.gr/s/20060269/Apple-iPhone-11-4GB-64GB-Black.html', 'img': 'https://c.scdn.gr/images/sku_main_images/020060/20060269/large_20190916100719_apple_iphone_11_64gb.jpeg', 'star_rating': '4.6'}, {'link': 'https://www.skroutz.gr/s/31694271/Realme-8i-Dual-SIM-4GB-128GB-Space-Black.html', 'img': 'https://d.scdn.gr/images/sku_main_images/031694/31694271/large_20211014134513_realme_8i_128gb_space_black.jpeg', 'star_rating': '4.6'}, {'link': 'https://www.skroutz.gr/s/32504283/Realme-GT-Neo-2-5G-Dual-SIM-8GB-128GB-Neo-Black.html', 'img': 'https://b.scdn.gr/images/sku_main_images/032504/32504283/large_20211122115729_realme_gt_neo_2_5g_8gb_128gb_neo_black.jpeg', 'star_rating': '4.7'}, 
{'link': 'https://www.skroutz.gr/s/31105224/Apple-iPhone-13-Pro-Max-5G-6GB-128GB-Graphite.html', 'img': 'https://a.scdn.gr/images/sku_main_images/031105/31105224/large_20210920153335_apple_iphone_13_pro_max_128gb_graphite.jpeg', 'star_rating': '4.5'}, {'link': 'https://www.skroutz.gr/s/31694187/Realme-8i-Dual-SIM-4GB-64GB-Space-Black.html', 'img': 'https://d.scdn.gr/images/sku_main_images/031694/31694187/large_20211014134219_realme_8i_64gb_space_black.jpeg', 'star_rating': '4.6'}, {'link': 'https://www.skroutz.gr/s/30717392/Samsung-Galaxy-A52s-5G-Dual-SIM-6GB-128GB-Awesome-White.html', 'img': 'https://d.scdn.gr/images/sku_main_images/030717/30717392/large_20210901102946_samsung_galaxy_a52s_128gb_awesome_white.jpeg', 'star_rating': '4.4'}

... 很快

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM