专业编程基础技术教程

网站首页 > 基础教程 正文

新必应用python写一个京东抢茅台的脚本,太实用了

ccvgpt 2024-12-19 11:50:07 基础教程 2 ℃

最近玩新必应比较上瘾,闲来无事的时候刷京东看到抢茅台呢,就试试看,结果老是失败,就想着让新必应用python写一个京东抢茅台的脚本,由于我是非科班,也不知道这个脚本能不能运行,就贴出来给大神们看看:

# coding=utf-8

新必应用python写一个京东抢茅台的脚本,太实用了

import requests

import time

import json

import random

import datetime


# 请填写以下信息

username = "" # 京东账号

password = "" # 京东密码

eid = "" # 浏览器F12获取

fp = "" # 浏览器F12获取

sku_id = "100012043978" # 茅台商品ID

buy_time = "2021-01-08 10:00:00" # 抢购时间


# 登录请求头

login_headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',

'Referer': 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F',

'Connection': 'keep-alive',

}


# 抢购请求头

buy_headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',

'Referer': f'https://item.jd.com/{sku_id}.html',

'Connection': 'keep-alive',

}


# 登录会话

session = requests.session()


# 获取登录二维码

def get_qrcode():

url = "https://qr.m.jd.com/show"

params = {

"appid": 133,

"size": 147,

"t": int(time.time() * 1000),

}

response = session.get(url=url, params=params, headers=login_headers)

if response.status_code == 200:

with open("qrcode.png", "wb") as f:

f.write(response.content)

print("请打开qrcode.png扫码登录")

else:

print("获取二维码失败")


# 检查是否扫码登录成功

def check_login():

url = "https://qr.m.jd.com/check"

    params = { "callback": 
f"jQuery{random.randint(1000000, 9999999)}",
        
"appid": 
133,
        
"token": session.cookies.get(
"wlfstk_smdl"),
        
"_": 
int(time.time() * 
1000),
    }
    response = session.get(url=url, params=params, headers=login_headers)
    
if response.status_code == 
200:
        result = response.text
        
if 
"二维码未扫描" 
in result:
            
print(
"请扫描二维码")
            
return 
False
        
elif 
"二维码已扫描" 
in result:
            
print(
"请在手机上确认登录")
            
return 
False
        
elif 
"二维码已过期" 
in result:
            
print(
"二维码已过期,请重新获取")
            
return 
False
        
elif 
"success" 
in result:
            
print(
"登录成功")
            
return 
True
        
else:
            
print(
"未知错误")
            
return 
False
    
else:
        
print(
"检查登录失败")
        
return 
False
# 获取用户信息
def 
get_user_info():
    url = 
"https://passport.jd.com/user/petName/getUserInfoForMiniJd.action"
    params = {
        
"callback": 
f"jQuery{random.randint(1000000, 9999999)}",
        
"_": 
int(time.time() * 
1000),
    }
    response = session.get(url=url, params=params, headers=login_headers)
    
if response.status_code == 
200:
        result = response.text
        start = result.find(
"(") + 
1
        end = result.rfind(
")")
        user_info = json.loads(result[start:end])
        nickname = user_info.get(
"nickName")
        
print(
f"欢迎您,{nickname}")
    
else:
        
print(
"获取用户信息失败")
# 预约商品
def 
reserve():
    url = 
"https://yushou.jd.com/youshouinfo.action"
    params = {
        
"callback": 
f"fetchJSON{random.randint(1000000, 9999999)}",
        
"sku": sku_id,
        
"_": 
int(time.time() * 
1000),
    }
    response = session.get(url=url, params=params, headers=buy_headers)
    
if response.status_code == 
200:
        result = response.text
start = result.find(
"(") + 
1
        end = result.rfind(
")")
        reserve_info = json.loads(result[start:end])
        
if reserve_info.get(
"code") == 
"200":
            reserve_url = reserve_info.get(
"url")
            session.get(url=reserve_url, headers=buy_headers)
            
print(
"预约成功")
        
else:
            
print(
"预约失败")
    
else:
        
print(
"获取预约信息失败")
# 秒杀商品
def 
seckill():
    url = 
"https://marathon.jd.com/seckillnew/orderService/pc/submitOrder.action?skuId=" + sku_id
    data = {
        
"skuId": sku_id,
        
"num": 
1,
        
"password": 
"",
        
"eid": eid,
        
"fp": fp,
    }
    response = session.post(url=url, data=data, headers=buy_headers)
    
if response.status_code == 
200:
        result = response.json()
        
if result.get(
"success"):
            order_id = result.get(
"orderId")
            total_price = result.get(
"totalMoney")
            pay_url = 
f"https://marathon.jd.com/seckill/seckill.action?skuId={sku_id}&orderId={order_id}"
            
print(
f"抢购成功,订单号:{order_id},总价:{total_price},付款链接:{pay_url}")
            
return 
True
        
else:
            
print(
f"抢购失败,返回信息:{result}")
            
return 
False
    
else:
        
print(
"提交订单失败")
        
return 
False
# 检查订单
def 
check_order():
    url = 
"https://order.jd.com/center/list.action"
    params = {
        
"rid": 
int(time.time() * 
1000),
    }
    response = session.get(url=url, params=params, headers=buy_headers)
    
if response.status_code == 
200:
        orders = response.text
        
if sku_id 
in orders:
            
print(
"订单提交成功")
            
return 
True
        
else:
            
print(
"订单提交失败")
            
return 
False
    
else:
        
print(
"获取订单失败")
        
return 
False
# 支付订单
def 
pay_order():
    
pass 
# 此处省略支付逻辑,需要在手机上完成
# 主函数
def 
main():
    
# 获取登录二维码
    get_qrcode()
    
# 检查是否登录成功
    
while 
not check_login():
        time.sleep(
1)
    
# 获取用户信息
    get_user_info()
    
# 预约商品
    reserve()
    
# 等待抢购时间
    
while 
True:
        now = datetime.datetime.now()
        
if now.strftime(
"%Y-%m-%d %H:%M:%S") >= buy_time:
            
break
        time.sleep(
0.1)
    
# 秒杀商品
    
while 
not seckill():
        time.sleep(
0.1)
    
# 检查订单
    
while 
not check_order():
        time.sleep(
0.1)
    # 支付订单
pay_order()
    
print(
"抢购流程结束")
if __name__ == 
"__main__":
    main()


以上就是新必应写的完整的脚本代码。

Tags:

最近发表
标签列表