[python]代码库
1、环境安装
pip3 install selenium
geckodriver-v0.27.0-win64.zip
#下载地址:https://github.com/mozilla/geckodriver/releases
#下载解压 拷贝geckodriver.exe到火狐浏览器安装目录C:\Program Files\Mozilla Firefox\ 下
2、清空购物车
3、加购商品
#!/usr/bin/env python#
# -*- coding:utf-8 -*-
from selenium import webdriver
import datetime
import time
driver = webdriver.Firefox()
def auto_buy(username, password, purchase_list_time):
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "打开登陆界面")
driver.get("https://passport.jd.com/new/login.aspx")
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "开始填写账号密码")
driver.find_element_by_link_text("账户登录").click()
driver.find_element_by_name("loginname").send_keys(username)
driver.find_element_by_name("nloginpwd").send_keys(password)
driver.find_element_by_id("loginsubmit").click()
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "手动拼图验证")
time.sleep(10) #此处睡眠时间用来手动拼图验证
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"登陆成功")
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "等待时间到达抢购时间:",purchase_list_time, "......")
while True:
count = 0
for buytime in purchase_list_time:
nowtime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if nowtime == buytime:
try:
count += 1
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "开始第 %s 次抢购......"%count)
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "打开购物车并选中商品")
driver.get("https://cart.jd.com/cart.action") # 打开购物车并选中商品
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "点击去结算")
driver.find_element_by_link_text("去结算").click() # 去结算
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "点击提交订单")
time.sleep(5) #提交订单前必须等待几秒【感觉跟电脑性能快慢有关,不卡的电脑可以适当降低尝试】
driver.find_element_by_id('order-submit').click() #提交订单
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),"订单提交成功,请前往订单中心待付款付款")
print("")
continue
except Exception as e:
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), "抢购出现异常,重新抢购: ", e)
continue
time.sleep(0.001)
purchase_list_time = [
"2022-01-10 00:00:00",
"2022-01-10 00:00:01",
"2022-01-10 00:00:02",
"2022-01-10 00:00:03",
"2022-01-10 00:00:04",
"2022-01-10 00:00:05",
]
auto_buy('账号', '密码', purchase_list_time)