本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
作者:python技术
PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取http://t.cn/A6Zvjdun
618!京东 PC 版抢卷
618不到一个月了,京东 618 活动已经开始了,不知道小伙伴们参与了没。先抢一下京东的优惠劵,也许 618 出现神劵呢?
获取二维码图片
这里我们使用二维码登录,通过浏览器的控制台可以看出有个带 show 的 url 地址可以得到二维码登录图片
首先导入所有需要的包和一个解析 json 的方法
import requests
import random
import time
import os
import json
from PIL import Image
def parse_json(str):
return json.loads(str[str.find('{'):str.rfind('}') + 1])
显示京东登录二维码图片
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
session = requests.session()
def show_QRcode():
url = 'https://qr.m.jd.com/show'
params = {
'appid': 133,
'size': 147,
't': str(int(time.time() * 1000)),
}
headers = {
'User-Agent': user_agent,
'Referer': 'https://passport.jd.com/new/login.aspx',
}
resp = session.get(url=url, headers=headers, params=params)
QRcode_path = 'QRcode.png'
with open(QRcode_path, 'wb') as f:
for chunk in resp.iter_content(chunk_size=1024):
f.write(chunk)
QRcode = Image.open(QRcode_path)
QRcode.show()
获取二维码图片之后,在 Fiddler 抓包神器中一直刷新带 check 的 url 地址,猜测这个也是和登录有关的,应该是检测二维码是否被扫码。
检测是否被扫码抓包
def check_QRcode():
url = 'https://qr.m.jd.com/check'
params = {
'appid': '133',
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'token': session.cookies.get('wlfstk_smdl'),
'_': str(int(time.time() * 1000)),
}
headers = {
'User-Agent': user_agent,
'Referer': 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F',
}
resp = session.get(url=url, headers=headers, params=params)
resp_json = parse_json(resp.text)
if 'ticket' in resp_json:
print(resp_json)
return resp_json['ticket']
else:
print(resp_json['msg'])
print('请刷新京东登录二维码!')
os._exit(0)
示例结果
{
'code': 200, 'ticket': 'AAEAIPL-bkU4RNrUw7YaLqYZhjWKqvP23PtkY1XTD2Cv2a52'}
验证二维码
在检测二维码是否被扫码之后,在 Fiddler 抓包神器中出现有一个带 qrCodeTicketValidation 的 url 地址,需要传递一个 t 参数,这个参数正好是 https://qr.m.jd.com/check
的返回值
验证二维码抓包
def validation_QRcode(ticket):
url = 'https://passport.jd.com/uc/qrCodeTicketValidation'
headers = {
'User-Agent': user_agent,
'Referer': 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F',
}
params={
't': ticket
}
resp = session.get(url=url, headers=headers, params=params)
print(resp.text)
示例结果
{
"returnCode":0,"url":"https://www.jd.com/"}
到这里京东二维码已经登录成功了
可以看到这个 url 的参数有 page,pageSize,不用猜都知道这是刷新的优惠券列表,抓它
def coupon_list():
url = 'https://a.jd.com/indexAjax/getCouponListByCatalogId.html'
headers = {
'User-Agent': user_agent,
'Referer': 'https://a.jd.com/?cateId=118',
}
couponList = []
for i in range(1, 20):
params = {
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'catalogId': '118',
'page': str(i),
'pageSize': '9',
'_': str(int(time.time() * 1000)),
}
try:
resp = session.get(url=url, params=params, headers=headers)
json = parse_json(resp.text)
couponList.extend(json['couponList'])
if json['totalNum'] == 1:
continue
else:
break
except Exception:
print('出错了!')
return couponList
示例结果就不写了,返回的 json 串太长了
最后一步领取优惠劵,在浏览器控制台中 url 地址上有一个 key,这个 key 存在与优惠劵列表的返回值中
最后一步,领取优惠券
def get_coupon(coupon_list):
url = 'https://a.jd.com/indexAjax/getCoupon.html'
headers = {
'User-Agent': user_agent,
'Referer': 'https://a.jd.com/?cateId=118',
}
for coupon in coupon_list:
params = {
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'key': coupon['key'],
'type': '1',
'_': str(int(time.time() * 1000)),
}
time.sleep(1)
resp = session.get(url=url, params=params, headers=headers)
print(resp.text)
示例结果
jQuery1912666({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery3381540({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery6247320({
"code":"16","success":false,"message":"本时段优惠券已抢完,请14:00再来吧!"})
jQuery5888701({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery5048959({
"code":"14","success":false,"message":"您已经参加过此活动,别太贪心哟,下次再来~"})
jQuery8608381({
"code":"14","success":false,"message":"您已经参加过此活动,别太贪心哟,下次再来~"})
jQuery2539212({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery6439595({
"code":"999","success":true,"message":"领券成功"})
jQuery2972325({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery1697862({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery1905738({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery7639432({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery8239706({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
jQuery1221489({
"code":"15","success":false,"message":"您今天已经参加过此活动,别太贪心哟,明天再来~"})
import requests
import random
import time
import os
import json
from PIL import Image
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
session = requests.session()
def show_QRcode():
url = 'https://qr.m.jd.com/show'
params = {
'appid': 133,
'size': 147,
't': str(int(time.time() * 1000)),
}
headers = {
'User-Agent': user_agent,
'Referer': 'https://passport.jd.com/new/login.aspx',
}
resp = session.get(url=url, headers=headers, params=params)
QRcode_path = 'QRcode.png'
with open(QRcode_path, 'wb') as f:
for chunk in resp.iter_content(chunk_size=1024):
f.write(chunk)
QRcode = Image.open(QRcode_path)
QRcode.show()
def check_QRcode():
url = 'https://qr.m.jd.com/check'
params = {
'appid': '133',
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'token': session.cookies.get('wlfstk_smdl'),
'_': str(int(time.time() * 1000)),
}
headers = {
'User-Agent': user_agent,
'Referer': 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F',
}
resp = session.get(url=url, headers=headers, params=params)
resp_json = parse_json(resp.text)
if 'ticket' in resp_json:
print(resp_json)
return resp_json['ticket']
else:
print(resp_json['msg'])
print('请刷新JD登录二维码!')
os._exit(0)
def validation_QRcode(ticket):
url = 'https://passport.jd.com/uc/qrCodeTicketValidation'
headers = {
'User-Agent': user_agent,
'Referer': 'https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F',
}
params={
't': ticket
}
resp = session.get(url=url, headers=headers, params=params)
print(resp.text)
def parse_json(str):
return json.loads(str[str.find('{'):str.rfind('}') + 1])
def coupon_list():
url = 'https://a.jd.com/indexAjax/getCouponListByCatalogId.html'
headers = {
'User-Agent': user_agent,
'Referer': 'https://a.jd.com/?cateId=118',
}
couponList = []
for i in range(1, 20):
params = {
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'catalogId': '118',
'page': str(i),
'pageSize': '9',
'_': str(int(time.time() * 1000)),
}
try:
resp = session.get(url=url, params=params, headers=headers)
json = parse_json(resp.text)
couponList.extend(json['couponList'])
if json['totalNum'] == 1:
continue
else:
break
except Exception:
print('出错了!')
return couponList
def get_coupon(coupon_list):
url = 'https://a.jd.com/indexAjax/getCoupon.html'
headers = {
'User-Agent': user_agent,
'Referer': 'https://a.jd.com/?cateId=118',
}
for coupon in coupon_list:
params = {
'callback': 'jQuery{}'.format(random.randint(1000000, 9999999)),
'key': coupon['key'],
'type': '1',
'_': str(int(time.time() * 1000)),
}
time.sleep(1)
resp = session.get(url=url, params=params, headers=headers)
print(resp.text)
if __name__ == '__main__':
show_QRcode()
time.sleep(10)
ticket = check_QRcode()
validation_QRcode(ticket)
coupon_list = coupon_list()
get_coupon(coupon_list)
京东PC版抓取优惠券到这里就结束了,代码还有不完善的地方,比如每次都要扫码登录,没有保存 cookies 自动登录京东等待。小伙伴们可以根据自身需求更改代码,造出适合自己的轮子。