ITPub博客

首页 > IT职业 > IT生活 > 京东抢购失败?试试用 python 准时自动抢购 (注释详尽)

京东抢购失败?试试用 python 准时自动抢购 (注释详尽)

IT生活 作者:专注的阿熊 时间:2023-01-06 17:10:44 0 删除 编辑

import cv2

import time

import random

import datetime

import numpy as np

from urllib import request

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

# 移动滑动验证码中的滑块

def checkMove(button_slide, distance):

     dist_finished = 0  # 已经移动的距离

     dist_remaining = distance  # 剩余的距离

     dist_move = 5  # 每次移动的距离

     element = driver.find_element_by_xpath(button_slide)  # 选取滑动验证码滑块

     ActionChains(driver).click_and_hold(element).perform()  # 模拟鼠标在滑块上点击并保持

     # 模拟不断加速移动滑块

     while dist_remaining > 0:

         dist_move += dist_move  # 不断 加速移动滑块

         # 每次移动滑块都带有正负偏差来模拟手动移动时的滑动不稳定

         ActionChains(driver).move_by_offset(dist_move, random.randint(-3, 3)).perform()  # 模拟鼠标水平向右拖动滑块

         dist_remaining -= dist_move  # 剩余距离减去已移动的距离

         dist_finished += dist_move  # 已完成距离加上已移动的距离

     ActionChains(driver).move_by_offset(dist_remaining, random.randint(-3, 3)).perform()  # 模拟鼠标水平回移拖动滑块修正

     ActionChains(driver).release( # 模拟松开鼠标

# 获取滑动验证码构成的两张图片并计算应移动的距离

def getCheckImage():

     image_big_path = r'//div/div[@class="JDJRV-bigimg"]/img'  # 滑动验证码大图(大背景)

     image_small_path = r'//div/div[@class="JDJRV-smallimg"]/img'  # 滑动验证码小图(小滑块)

     button_slide = '//div[@class="JDJRV-slide-inner JDJRV-slide-btn"]'  # 滑动验证码滑块按钮

     image_big = driver.find_element_by_xpath(image_big_path).get_attribute("src")  # 验证码背景图的完整路径

     image_small = driver.find_element_by_xpath(image_small_path).get_attribute("src")  # 验证码滑块图的完整路径

     request.urlretrieve(image_big, 'background.jpg')  # 下载验证码背景图到本地

     request.urlretrieve(image_small, 'slideblock.jpg')  # 下载验证码滑块图到本地

     cv2.imwrite('background.jpg', cv2.imread('background.jpg', 0))  # 将验证码背景图读取为灰度图并覆盖原图

     slideblock = cv2.imread('slideblock.jpg', 0)  # 将验证码滑块图读取为灰度图

     slideblock = abs(255 - slideblock)  # 对验证码滑块图反灰化处理

     cv2.imwrite('slideblock.jpg', slideblock)  # 保存处理后的验证码滑块图

     background = cv2.imread('background.jpg')  # 读取验证码背景图(灰度)

     slideblock = cv2.imread('slideblock.jpg')  # 读取验证码滑块图(灰度)

     result = cv2.matchTemplate(background, slideblock, cv2.TM_CCOEFF_NORMED)  # 模板匹配,获得滑块在背景上的相似度矩阵

     _, distance = np.unravel_index(result.argmax(), result.shape)  # 获得要移动的距离

     return button_slide, distance

# 滑动验证

def slideIdentify():

     slideButton, distance = getCheckImage()  # 获取滑块和滑块需要移动的距离

     print(f' 本次滑块需要移动的距离为: {distance}')  # 打印滑块需要移动的距离

     checkMove(slideButton, distance / 1.3)  # 移动滑块, 1.3 是一个实验修正值

# 登录京东网页版

def login(username, password):

     driver.get('https://passport.jd.com/new/login.aspx')  # 京东登录界面链接

     driver.implicitly_wait(2)  # 隐式等待 2 秒

     driver.find_element_by_link_text(" 账户登录 ").click()  # 找到账户登录并点击

     driver.implicitly_wait(2)  # 隐式等待 2 秒

     driver.find_element_by_id("loginname").send_keys(username)  # 找到用户名输入框并输入用户名

     driver.implicitly_wait(2)  # 隐式等待 2 秒

     driver.find_element_by_id("nloginpwd").send_keys(password)  # 找到密码输入框输入密码

     driver.implicitly_wait(2)  # 隐式等待 2 秒

     driver.find_element_by_id("loginsubmit").click()  # 找到登录并点击

     while True:

         try:

             slideIdentify()  # 进行滑动验证

             time.sleep(2)  # 等待 3 秒

         except:

             print(" 登录成功 ")

             break

# 定时购买东西

def buy(buy_time):

     driver.implicitly_wait(2)  # 隐式等待 2 秒

     driver.find_element_by_link_text(" 我的购物车 ").click()  # 找到我的购物车并点击

     total_windows =跟单网gendan5.com driver.window_handles  # 所有打开的窗口

     driver.switch_to.window(total_windows[1])  # 句柄迁移到第二个窗口

     while True:

         current_time = datetime.datetime.now()  # 获取当前日期时间

         if current_time.strftime('%Y-%m-%d %H:%M:%S') == buy_time:  # 如果当前时间等于指定购买时间

             driver.implicitly_wait(2)  # 隐式等待 2 秒

             driver.find_element_by_name('select-all').click()  # 购物车全选

             time.sleep(0.5)  # 等待 0.5 秒

             driver.find_element_by_link_text(" 去结算 ").click()  # 找到去结算并点击

             driver.implicitly_wait(2)  # 隐式等待 2 秒

             driver.find_element_by_id("order-submit").click()  # 找到提交订单并点击

             driver.implicitly_wait(2)  # 隐式等待 2 秒

             print('current time : ' + current_time.strftime('%Y-%m-%d %H:%M:%S'))  # 打印当前时间

             print(' 购买成功 !')  # 购买成功

if __name__ == '__main__':

     driver = webdriver.Edge(executable_path='./msedgedriver.exe')  # 打开 Edge 浏览器

     driver.maximize_window()  # 最大化 Edge 浏览器窗口

     login(' 你的用户名 ', ' 你的密码 ')  # 登录京东

     buy('2021-08-14 12:00:00')  # 定时购买

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2931188/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    410
  • 访问量
    276757