selenium 版本4爬取淘宝商品信息

发布时间 2023-10-17 11:43:27作者: Me-lihu
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from time import sleep
#访问淘宝页面
path =  Service('C:\Python38\chromedriver.exe')
browser = webdriver.Chrome(service=path)
dr = webdriver.Chrome()
dr.get('https://www.taobao.com/')

#输入搜索信息
dr.find_element(By.CSS_SELECTOR,'#q').send_keys("iphone")
#全局等待15秒
dr.implicitly_wait(15)
#窗口最大化
dr.maximize_window()
#回车
dr.find_element(By.CSS_SELECTOR,'#q').send_keys(Keys.ENTER)
sleep(3)
#取出3页的商品
for i in range(3):
    # 取出一页每个商品信息
    iphonelist=dr.find_elements(By.CSS_SELECTOR,".Content--contentInner--QVTcU0M .Card--doubleCardWrapper--L2XFE73")
    for d in iphonelist:
        print(d.find_element(By.CSS_SELECTOR,".ShopInfo--TextAndPic--yH0AZfx a").text)
        print(d.find_element(By.CSS_SELECTOR,".Title--title--jCOPvpf span").text)
        print(d.find_element(By.CSS_SELECTOR,".Price--priceWrapper--Q0Dn7pN .Price--unit--VNGKLAP").text + d.find_element(By.CSS_SELECTOR,".Price--priceWrapper--Q0Dn7pN .Price--priceInt--ZlsSi_M").text + d.find_element(By.CSS_SELECTOR,".Price--priceWrapper--Q0Dn7pN .Price--priceFloat--h2RR0RK").text )
        # print(d.find_element(By.CSS_SELECTOR,".Price--priceWrapper--Q0Dn7pN  .Price--realSales--FhTZc7U").text)
    #取下一页按钮
    next=dr.find_element(By.XPATH,"//*[text()='下一页']")
    next.click()