# -*- coding:utf8 -*-
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class TaobaoBalenciaga:
def __init__(self, user, passwd):
self.user = user
self.passwd = passwd
"""
打开淘宝 绕过浏览器检测
"""
driver = webdriver.Chrome()
driver.get('https://www.taobao.com/')
# 打开页面优先执行的js,execute_cdp_cmd
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
def login(self):
_input = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="q"]'))
)
_input.send_keys('巴黎世家丝袜')
time.sleep(1)
_click = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="J_TSearchForm"]/div[1]/button'))
)
_click.click()
time.sleep(1)
username = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="fm-login-id"]'))
)
username.send_keys(self.user)
time.sleep(1)
password = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="fm-login-password"]'))
)
password.send_keys(self.passwd)
time.sleep(1)
button = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="login-form"]/div[4]/button'))
)
button.click()
time.sleep(1)
def product_details(self):
divs = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_all_elements_located((By.XPATH, '//*[@id="mainsrp-itemlist"]/div/div/div[1]/div'))
)
for div in divs:
picture = div.find_element(By.XPATH, './/div[@class="pic-box J_MouseEneterLeave J_PicBox"]//a/img').get_attribute('data-src')
print(div.find_element(By.XPATH, './/div[@class="pic-box J_MouseEneterLeave J_PicBox"]//a/img').get_attribute('data-src'))
price = div.find_element(By.XPATH, './/strong').text
print(div.find_element(By.XPATH, './/strong').text)
deatils = div.find_element(By.XPATH, './/div[@class="pic-box J_MouseEneterLeave J_PicBox"]//a').get_attribute('href')
print(div.find_element(By.XPATH, './/div[@class="pic-box J_MouseEneterLeave J_PicBox"]//a').get_attribute('href'))
sales = div.find_element(By.XPATH, './/div[@class="deal-cnt"]').text
print(div.find_element(By.XPATH, './/div[@class="deal-cnt"]').text)
title = div.find_element(By.XPATH, './/div[@class="row row-2 title"]/a').text
print(div.find_element(By.XPATH, './/div[@class="row row-2 title"]/a').text)
location = div.find_element(By.XPATH, './/div[@class="row row-3 g-clearfix"]/div[@class="location"]').text
print(div.find_element(By.XPATH, './/div[@class="row row-3 g-clearfix"]/div[@class="location"]').text)
def next_page(self):
self.login()
while True:
try:
next_page = WebDriverWait(self.driver, 5, 0.5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="mainsrp-pager"]/div/div/div/ul/li[8]/a'))
)
next_page.click()
time.sleep(4)
except Exception:
self.driver.quit()
self.product_details()
print('--'*30)
# self.driver.quit()
if __name__ == '__main__':
# tab = TaobaoBalenciaga('用户名','密码')
tab = TaobaoBalenciaga('w','wz291')
tab.next_page()
python selenium获取淘宝商品信息
最新推荐文章于 2024-07-20 17:12:48 发布