网站首页 > 基础教程 正文
最近玩新必应比较上瘾,闲来无事的时候刷京东看到抢茅台呢,就试试看,结果老是失败,就想着让新必应用python写一个京东抢茅台的脚本,由于我是非科班,也不知道这个脚本能不能运行,就贴出来给大神们看看:
# coding=utf-8
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()
以上就是新必应写的完整的脚本代码。
猜你喜欢
- 2024-12-19 手把手教你用python抢京东大额神券
- 2024-12-19 SpringCloud系列——SSO 单点登录
- 2024-12-19 学习Java半年时间可以到哪种程度 学java半年能找到工作吗
- 2024-12-19 前端工程师都会遇到的nodejs常见问题和解决方案复盘
- 2024-12-19 Java后端学习路线梳理 java后端要掌握的知识
- 2024-12-19 真正的0基础如何学JAVA 零基础学java从哪里开始
- 2024-12-19 知了堂Java培训怎么样,都学习哪些内容
- 2024-12-19 Python爬虫大佬的万字长文总结,requests与selenium操作合集
- 2024-12-19 究竟是多深的功力!能一举拿下字节/腾讯/蚂蚁三大厂的前端offer
- 2024-12-19 Python爬虫神器:PyQuery,解析网页更简单,小白也能学会
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)