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

python -- 使用selenium模拟登录淘宝,爬取商品信息

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

python -- 使用selenium模拟登录淘宝,爬取商品信息

目录
  • 环境
  • 谷歌驱动下载
  • 解析
  • 结果
  • 代码

环境
  • windows10
  • python3.7.3
  • selenium
  • 谷歌
  • 谷歌驱动
谷歌驱动下载

http://chromedriver.storage.googleapis.com/index.html

下载与自己电脑谷歌浏览器版本相应的谷歌驱动

解析

在这里主要爬去商品的基本信息:

结果

代码
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
import csv
f = open('./taobao.csv','a',newline='')
writer  = csv.writer(f)

def login(driver):
    driver.delete_all_cookies()
    url = "https://www.taobao.com/" #https://www.qcc.com/weblogin?back=%2F
    driver.get(url)
    time.sleep(10)
    while True:
        try:
            driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/ul[2]/li[2]/div[1]/a').click()
            break
        except:
            time.sleep(5)
    while True:
        try:
            driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div/div[2]/div/form/div[1]/div[2]/input').send_keys('账号')
            driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div/div[2]/div/form/div[2]/div[2]/input').send_keys('密码')
            time.sleep(5)
            break
        except:
            time.sleep(5)
    driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div/div[2]/div/form/div[4]/button').click()
    time.sleep(50)
    driver.find_element_by_xpath('/html/body/div[1]/div/ul[2]/li[1]/div/a').click()
    time.sleep(20)
    while True:
        try:
            driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div/div[1]/div[2]/form/div[2]/div[3]/div/input').send_keys('红酒')#input('输入爬取信息关键字:')
            driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div/div[1]/div[2]/form/div[1]/button').click()
            break
        except:
            time.sleep(5)
    time.sleep(10)
    page = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[1]/div[16]/div/div[1]/div/div[2]/ul/li[2]').text.split('/')[1]
    print(page)
    time.sleep(10)

    for _ in range(int(float(page))-1):
        for i in range(1,49):
            try:
                test = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[1]/div[21]/div/div/div[1]/div[{}]/div[2]'.format(i)).text.split('n')
                print(test)
                writer.writerow(test)
            except:
                pass
        time.sleep(3)
        try:
            driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[1]/div[26]/div/div/div/ul/li[8]/a').click()
        except:
            try:
                driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[1]/div[26]/div/div/div/ul/li[9]/a').click()
            except:
                try:
                    driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[3]/div[1]/div[26]/div/div/div/ul/li[10]/a').click()
                except:
                    try:
                        driver.find_element_by_xpath(
                        '/html/body/div[1]/div[2]/div[3]/div[1]/div[26]/div/div/div/ul/li[11]/a').click()
                    except:
                        try:
                            driver.find_element_by_xpath(
                                '/html/body/div[1]/div[2]/div[3]/div[1]/div[26]/div/div/div/ul/li[12]/a').click()
                        except:
                            pass
        time.sleep(10)
        try:
            a = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/div[2]/center/div[1]/div/div[1]/div[2]/span').text
        except:
            a = []
        print(a)
        if '滑块' in a:
            button = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[1]/div[2]/center/div[1]/div/div[1]/span')
            # 滑动滑块
            ActionChains(driver).click_and_hold(button).perform()
            # 拉动滑块
            ActionChains(driver).move_by_offset(xoffset=258, yoffset=0).perform()
            ActionChains(driver).release().perform()

        time.sleep(10)

    driver.close()
    f.close()

def main():
    # while True:
        """
        chromeOptions 是一个配置 chrome 启动是属性的类,就是初始化
        """
        option = webdriver.ChromeOptions()
        """
        add_experimental_option 添加实验性质的设置参数
        """
        option.add_experimental_option('excludeSwitches', ['enable-automation'])  # webdriver防检测
        '''
        add_argument 添加启动参数
        '''
        option.add_argument("--disable-blink-features=AutomationControlled")
        option.add_argument("--no-sandbox")
        option.add_argument("--disable-dev-usage")
        """
        Chrome 配置驱动
        """
        driver = webdriver.Chrome(executable_path=r'/home/zc/桌面/chromedriver',options=option)  # 在这里更换自己的谷歌驱动的地址
        driver.set_page_load_timeout(15)
        login(driver)
        # jugesd(driver)


if __name__ == '__main__':
    main()


希望这篇文章对你有用!
谢谢点赞评论!

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

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

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