爬虫实战之selenium淘宝抢购订单

    基本原理: 主要利用selenium登陆淘宝,并对购物车内的商品提交,简单的一个抢购订单 用selenium正常登陆会出现滑动验证,发现用微博账号登陆就不会 代码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2020/06/19 18:40:34 
# @File    : 淘宝.py
# @Sortware: Vsc

# 秒杀软件
# 打开网址:https://www.taobao.com/
# 点击登陆
# 点击进入购物车:https://cart.taobao.com/cart.htm
# 全选商品
# 点击结算
# 点击提交订单,生成一个订单,代表商品已经抢购到了

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time,datetime

# 登陆
def login():
    # 进入淘宝
    driver.get(https://www.taobao.com/)
    # 定位登录按钮
    driver.find_element_by_class_name(h).click()

    # 1、账号密码登录,手动滑动验证
    # 输入账号
    # driver.find_element_by_name(fm-login-id).send_keys(账号)
    # 输入密码
    # driver.find_element_by_name(fm-login-password).send_keys(密码)
    # 点击登陆按钮
    # driver.find_element_by_class_name(fm-button).click()
    
    # 2、微博登陆,可以避免滑动验证
    driver.find_element_by_class_name(weibo-login).click()
    # 输入账号
    driver.find_element_by_name(username).send_keys(账号)
    # 输入密码
    driver.find_element_by_name(password).send_keys(密码)
    time.sleep(3)
    # 点击登陆按钮
    driver.find_element_by_xpath(//*[@id="pl_login_logged"]/div/div[7]/div[1]/a).click()

    time.sleep(5)
    # 进入购物车页面
    driver.get(https://cart.taobao.com/cart.htm)
    # 获取当前时间
    now = datetime.datetime.now()
    print(登陆成功,当前时间为,,now.strftime(%Y-%m-%d %H:%M:%S))

# 购买
def buy(times):
    while True:
        now = datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S.%f)
        # 现在的时间要大于抢购的时间
        if now > times:
            # 点击全选
            while True:
                try:
                    if driver.find_element_by_class_name(select-all):
                        driver.find_element_by_class_name(select-all).click()
                        break
                except:
                    print(找不到全选按钮)
                    
            time.sleep(2)
            # 点击结算
            while True:
                try:
                    if driver.find_element_by_xpath(//*[@id="J_Go"]/span):
                        driver.find_element_by_xpath(//*[@id="J_Go"]/span).click()
                        break
                except:
                    print(找不到结算按钮)
                    
            time.sleep(2)
            # 提交订单
            while True:
                try:
                    if driver.find_element_by_class_name(go-btn):
                        driver.find_element_by_class_name(go-btn).click()
                        break
                except:
                    print(找不到提交按钮)

if __name__ == __main__:
    times = input(请输入抢购的时间:)
    driver = webdriver.Chrome()
    login()
    buy(times)
经验分享 程序员 微信小程序 职场和发展