python selenium获取淘宝商品信息

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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值