栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python selenium获取淘宝商品信息

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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[@]//a/img').get_attribute('data-src')
            print(div.find_element(By.XPATH, './/div[@]//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[@]//a').get_attribute('href')
            print(div.find_element(By.XPATH, './/div[@]//a').get_attribute('href'))
            sales = div.find_element(By.XPATH, './/div[@]').text
            print(div.find_element(By.XPATH, './/div[@]').text)
            title = div.find_element(By.XPATH, './/div[@]/a').text
            print(div.find_element(By.XPATH, './/div[@]/a').text)
            location = div.find_element(By.XPATH, './/div[@]/div[@]').text
            print(div.find_element(By.XPATH, './/div[@]/div[@]').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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/700543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号