【淘宝】python的淘宝秒杀抢购下单源码参考

时间:2022-07-22
本文章向大家介绍【淘宝】python的淘宝秒杀抢购下单源码参考,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

疫情如期,隔离还在继续,何时工作是一个头大的问题,最近在看口罩,不少电商平台都有放出口罩,当然,手残党将会也会是一直难以下手,你可能很难抢得到,故找了几个关于python的淘宝秒杀抢购下单源码参考,当然本渣渣测试下单成功,但是准点抢购还是没有成功。

你是否有更好的方法或者方案?

关于淘宝下单的思路,鉴于淘宝的厉害,相信基本上都是采用的无头浏览器,即selenium驱动浏览器模拟人工提交订单,如果能够破解下单的post参数是最好不过了,暂未研究!

淘宝下单流程:

第一步:登录

发现微博登录添加了验证码,不知道是不是异地还是更新了,故采用扫码登录。

第二步:购买/结算

其中采用购物车提交的方式会让你感觉流畅不少!

当然也可以直接商品页购买!

第三步:提交订单

第四步:支付订单(秒杀下单)

这里准点下单购买能够走到第三步就成功了,当然还有一个思路就是试试看移动端下单是否更加简单顺畅呢?

python的淘宝秒杀抢购下单源码参考

版本一:

淘宝秒杀购物车下单支付

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import requests
from datetime import datetime
#来源:吾爱论坛 id:牵手丶若相惜

path =r"C:UsersAdministratorAppDataLocalProgramsPythonPython36chromedriver.exe"  # 谷歌chromedriver完整路径
# 输入秒杀时间
start_time = '2020-03-02 19:59:58'
#password = input("输入付款密码:")
print("你只有15秒的登录时间")
# 将输入的时间进行格式化
timeArray = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
# 用来判断 你是订单提交失败还是支付失败
sum = 0
# 设置chrome驱动的路径
driver = webdriver.Chrome(executable_path=path)
# 打开淘宝的登录界面
driver.get("https://cart.taobao.com/cart.htm")
# 最大化浏览器
driver.maximize_window()

# 判断全选框是否出现 出现则点击全选 否则继续等待 最多等待15秒
try:
    WebDriverWait(driver, 15, 0.1).until(
        lambda el: driver.find_element_by_xpath('//*[@id="J_SelectAll1"]/div/label')).click()
except:
    print("登录失败")


def time_server():
    # 获取淘宝服务器的时间戳
    r1 = requests.get(url='http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp',
                      headers={
                          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36'}
                      ).json()['data']['t']
    # 把时间戳格式/1000 获取毫秒
    timeNum = int(r1) / 1000
    # 格式化时间 (小数点后6为)
    time1 = datetime.fromtimestamp(timeNum)
    return time1


# 等待时间到预定的时间
print("等待中")

while True:
    # 判断时间服务器时间是否大于或等于输入的时间
    if time_server() >= timeArray:
        # 点击结算
        driver.find_element_by_xpath('//*[@id="J_Go"]').click()
        break
    else:
        continue

try:
    # 判断提交订单的按钮是否出现 出现就点击 否则继续等待 最多等待3秒
    WebDriverWait(driver, 3, 0.1).until(
        lambda el: driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a[2]')).click()
    print("订单提交成功")
    print("秒杀成功")
    sum = 1
    '''
    # 判断输入密码的框是否出现 出现就输入密码
    WebDriverWait(driver, 5, 0.1).until(
        lambda el: driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a[2]')).send_keys(password)
    # 点击确认付款
    driver.find_element_by_xpath('//*[@id="J_authSubmit"]').click()
    print("付款成功")'''
except:
    if sum == 0:
        print("提交订单失败")
    else:
        print("提交订单失败")

修改来源:

[Python] 开源一个python的淘宝秒杀的源码

吾爱论坛 id:牵手丶若相惜

版本二:

购物车提交订单

import os
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
import datetime
import time

# deckodreiver
chromedriver_path = r"C:UsersAdministratorAppDataLocalProgramsPythonPython36chromedriver.exe"  # 谷歌chromedriver完整路径
options = webdriver.ChromeOptions()  # 配置 chrome 启动属性
options.add_experimental_option("excludeSwitches",['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
driver=webdriver.Chrome(executable_path=chromedriver_path,options=options)
wait=WebDriverWait(driver,10) #超时时长为10s


#登录
def login():
    driver.get("https://cart.taobao.com/cart.htm")
    time.sleep(20)
    sel = wait.until(EC.presence_of_element_located((By.ID, 'J_SelectAll2')))
    sel.click()


#下单
def buy(buytime):
    i = 0
    while True:
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        if now > buytime:
            #driver.refresh()  # 刷新页面
            try:
                jss =wait.until(EC.presence_of_element_located((By.ID, 'J_Go')))
                jss.click()
                try:
                    tjdd =wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                    tjdd.click()
                    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    print(f"抢购成功,请尽快付款"+now)
                    break
                except:
                    i=i+1
                    try:
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"再次尝试提交订单" + now)
                        driver.refresh()  # 刷新页面
                        tjdd = wait.until(
                            EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                        tjdd.click()
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"抢购成功,请尽快付款"+now)
                    except:
                        try:
                            i = i + 1
                            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                            print(f"再次尝试提交订单" + now)
                            driver.refresh()  # 刷新页面
                            tjdd = wait.until(
                                EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                            tjdd.click()
                            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                            print(f"抢购成功,请尽快付款" + now)
                        except:
                            i = i + 1
                            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                            print(f"再次尝试提交订单" + now)
                            driver.refresh()  # 刷新页面
                            tjdd = wait.until(
                                EC.presence_of_element_located(
                                    (By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                            tjdd.click()
                            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                            print(f"抢购成功,请尽快付款" + now)
            except:
                driver.get("https://cart.taobao.com/cart.htm")
                sel = wait.until(EC.presence_of_element_located((By.ID, 'J_SelectAll2')))
                sel.click()
                jss = wait.until(EC.presence_of_element_located((By.ID, 'J_Go')))
                jss.click()
                now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                print(f"再次尝试提交订单"+now)
                i = i + 1
                try:
                    tjdd =wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                    tjdd.click()
                    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    print(f"抢购成功,请尽快付款"+now)
                    break
                except:
                    i = i + 1
                    try:
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"再次尝试提交订单" + now)
                        driver.refresh()  # 刷新页面
                        tjdd = wait.until(
                            EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                        tjdd.click()
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"抢购成功,请尽快付款"+now)
                        break
                    except:
                        driver.get("https://cart.taobao.com/cart.htm")
                        sel = wait.until(EC.presence_of_element_located((By.ID, 'J_SelectAll2')))
                        sel.click()
                        jss = wait.until(EC.presence_of_element_located((By.ID, 'J_Go')))
                        jss.click()
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"再次尝试提交订单" + now)
                        i = i + 1
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"再次尝试提交订单" + now)
                        driver.refresh()  # 刷新页面
                        tjdd = wait.until(
                            EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a[@class="go-btn"]')))
                        tjdd.click()
                        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                        print(f"抢购成功,请尽快付款" + now)
                        break

        if i>10:
            print(f">>>已经尝试第{i}次抢购,抢购失败,程序终止!")
            break

if __name__=="__main__":
    login()
    buy('2020-03-02 19:59:58')

修改来源:

[Python] 某宝秒杀关键模块及思路

吾爱论坛 id:露露的大雕

版本三:

商品页下单立即购买

#淘宝模拟登陆采集阿里商品
# -*- coding: utf-8 -*-
#20200302 by 微信:huguo00289
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
import time
import datetime

#定义一个taobao类
class taobao_infos:

    #对象初始化
    def __init__(self):
        url='https://login.taobao.com/member/login.jhtml'
        self.url=url

        options=webdriver.ChromeOptions() #配置 chrome 启动属性
        #options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2}) #不加载图片,加快访问速度
        options.add_experimental_option("excludeSwitches",['enable-automation']) # 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium

        self.browser=webdriver.Chrome(executable_path=chromedriver_path,options=options)
        self.wait=WebDriverWait(self.browser,10) #超时时长为10s

    #扫码登陆淘宝
    def login(self):
        #打开网页
        self.browser.get(self.url)
        time.sleep(1)
        time.sleep(15)


    #打开抢购商品首页
    def get_shop(self,shop_url,buytime):
        print("正在打开需要抢购的页面")
        self.browser.get(shop_url)
        while True:
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            if now > buytime:
                try:
                    #等待购买按钮出现
                    linkbuy=self.wait.until(EC.presence_of_element_located((By.ID,'J_LinkBuy')))
                    linkbuy.click()
                    sutj=self.wait.until(EC.presence_of_element_located((By.XPATH,'//div[@class="wrapper"]/a')))
                    sutj.click()
                    print(f"抢购成功,请尽快付款")
                except:
                    self.browser.refresh()  # 刷新页面
                    linkbuy = self.wait.until(EC.presence_of_element_located((By.ID, 'J_LinkBuy')))
                    linkbuy.click()
                    sutj = self.wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="wrapper"]/a')))
                    sutj.click()
                    print(f"抢购成功,请尽快付款")





    def gb(self):
        print(">>> 抢购完毕,关闭浏览器!")
        self.browser.quit()


if __name__ == '__main__':
    chromedriver_path = r"C:UsersAdministratorAppDataLocalProgramsPythonPython36chromedriver.exe"  #谷歌chromedriver完整路径
    spider=taobao_infos()
    spider.login()
    datetime='2020-03-02 11:59:58'
    shop_url="https://detail.tmall.com/item.htm?id=550189462849&price=24.8"
    #shop_url='https://detail.tmall.com/item.htm?id=612510400743' #抢购商品页
    try:
        spider.get_shop(shop_url,datetime)
    except:
        spider.get_shop(shop_url, datetime)
    spider.gb()

网速比较慢,测试正常下单是基本没问题,可是说到准点抢购下单,还是够呛,如果本地驱动浏览器打开网页秒开的话,是否有很大几率成功呢,这还得测试!!