-
Notifications
You must be signed in to change notification settings - Fork 109
/
jd.py
248 lines (190 loc) · 8.18 KB
/
jd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import requests
import random
import json
import os
import time
import re
from lxml import etree
from rk import *
url = 'https://passport.jd.com/new/login.aspx'
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
'ContentType': 'text/html; charset=utf-8',
'Accept-Encoding':'gzip, deflate, sdch',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection' : 'keep-alive',
}
s = requests.Session()
s.headers = headers
# 请求登录页面
req1 = s.get(url=url, headers=headers)
sel = etree.HTML(req1.content)
uuid = sel.xpath('//input[@id="uuid"]/@value')[0]
eid = sel.xpath('//input[@id="eid"]/@value')[0]
sa_token = sel.xpath('//input[@id="sa_token"]/@value')[0]
pubKey = sel.xpath('//input[@id="pubKey"]/@value')[0]
t = sel.xpath('//input[@id="token"]/@value')[0]
r = random.random()
login_url = 'https://passport.jd.com/uc/loginService'
class JD(object):
def __init__(self,username,password,rk_username,rk_pwd):
self.username = username
self.password = password
self.rkclient = RClient(rk_username,rk_pwd)
self.trackid = ''
self.pid = ''
# 账号登录函数
def login(self):
params = {
'uuid':uuid,
'eid':eid,
# 'fp':'a2fd52211772d8fea0515bedca560b0b',
'_t':t,
'loginType':'c',
'loginname':self.username,
'nloginpwd':self.password,
'chkRememberMe':'',
'authcode':'',
'pubKey':pubKey,
'sa_token':sa_token,
# 'seqSid':'5574250748814772000'
}
headers = {
'Referer':'https://passport.jd.com/uc/login?ltype=logout',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
# 验证码图片
imgcode = 'http:' + sel.xpath('//img[@id="JD_Verification1"]/@src2')[0]
img = requests.get(imgcode)
# 把这个路径替换成自己电脑jd.py文件夹的路径,/Users/zhangkai/Desktop/JD
with open('/Users/zhangkai/Desktop/JD/a.jpg', 'wb') as f:
f.write(img.content)
im = open('a.jpg','rb').read()
print('开始识别验证码...')
# print(imgcode) # 手动验证码连接
# 自动打码
imgcode1 = self.rkclient.rk_create(im, 3040)['Result']
print(imgcode1)
if imgcode != '':
# params['authcode'] = input('请输入验证码:') # 手动输验证码
params['authcode'] = str(imgcode1)
req2 = s.post(login_url, data=params, headers=headers)
patt = '<Cookie TrackID=(.*?) for .jd.com/>'
self.trackid = re.compile(patt).findall(str(s.cookies))
js = json.loads(req2.text[1:-1])
if js.get('success'):
print('登录成功')
else:
print('登录失败')
else:
req2 = s.post(login_url, data=params, headers=headers)
patt = '<Cookie TrackID=(.*?) for .jd.com/>'
self.trackid = re.compile(patt).findall(str(s.cookies))
js = json.loads(req2.text[1:-1])
if js.get('success'):
print('登录成功')
else:
print('登录失败')
def addcart(self):
self.pid = input('请输入要加入购物车的商品编号:')
pcount = input('请输入加入数量:')
add_carturl = 'https://cart.jd.com/gate.action?pid='+self.pid+'&pcount='+pcount+'&ptype=1'
# add_carturl = 'https://cart.jd.com/gate.action?pid=3659204&pcount=1&ptype=1'
req4 = s.get(add_carturl)
if re.compile('<title>(.*?)</title>').findall(req4.text)[0] == '商品已成功加入购物车':
print('商品已成功加入购物车')
else:
print('添加购物车失败')
def submit(self):
#购物车页面
carturl = 'https://cart.jd.com'
req5 = s.get(carturl)
# 取消选择某个商品
cancelitemurl = 'https://cart.jd.com/cancelItem.action?rd' + str(r)
form_data = {
'outSkus':'',
'pid':self.pid, # 商品id
'ptype': '1',
'packId': '0',
'targetId': '0',
'promoID': '0',
'locationId': '1-2810-6501-0' # 地址代码
}
req6 = s.post(cancelitemurl, data=form_data)
# 选择某个商品
selectitemurl = 'https://cart.jd.com/selectItem.action?rd' + str(r)
req7 = s.post(selectitemurl, data=form_data)
timestamp = int(time.time() * 1000)
# 订单结算页
orderInfo = 'https://trade.jd.com/shopping/order/getOrderInfo.action?rid=' + str(timestamp)
# 提交订单url
submitOrder = 'https://trade.jd.com/shopping/order/submitOrder.action'
submit_data = {
'overseaPurchaseCookies':'',
'submitOrderParam.sopNotPutInvoice':'false',
'submitOrderParam.trackID': self.trackid[0],
'submitOrderParam.ignorePriceChange': '0',
'submitOrderParam.btSupport': '0',
'submitOrderParam.eid': eid,
'submitOrderParam.fp': 'b31fc738113fbc4ea5fed9fc9811acc6',
# 'riskControl': 'D0E404CB705B9732D8D7A53159E363F2140ADCDE164C1F9CABA71F1D7552B70E5C9C6041832CEB4B',
}
ordertime = input('''请选择:
1.设置下单时间
2.选择立即下单(可用于监控库存,自动下单)
请输入选择(1/2):
''')
if ordertime == '1':
set_time = input('请按照2017-05-01 23:11:11格式输入下单时间:')
timeArray = time.mktime(time.strptime(set_time,'%Y-%m-%d %H:%M:%S'))
while True:
if time.time() >= timeArray:
print('正在提交订单...')
req8 = s.post(submitOrder, data=submit_data)
js1 = json.loads(req8.text)
print(js1)
# 判断是否下单成功
if js1['success'] == True:
print('下单成功!')
else:
print('下单失败')
break
else:
# print('等待下单...')
continue
# 直接下单
elif ordertime == '2':
while True:
area = '1_2810_6501_0' # 地址编码,这里请替换成自己地区的编码
stockurl = 'http://c0.3.cn/stock?skuId=' + self.pid + '&cat=652,829,854&area=' + area + '&extraParam={%22originid%22:%221%22}'
resp = s.get(stockurl)
jsparser = json.loads(resp.text)
# 33 有货 34 无货
if jsparser['stock']['StockState'] == 33 and jsparser['stock']['StockStateName'] == '现货':
print('库存状态:',jsparser['stock']['StockStateName'])
req8 = s.post(submitOrder, data=submit_data)
print('正在提交订单...')
js1 = json.loads(req8.text)
# 判断是否下单成功
if js1['success'] == True:
print('下单成功!')
break
else:
print('下单失败')
# 3秒后重新尝试下单,可自行修改时间间隔
time.sleep(3)
continue
elif jsparser['stock']['StockState'] != 33:
print('无货,监控中...')
time.sleep(3) # 请酌情修改时间间隔,最少1秒
continue
if __name__ == '__main__':
jd_user = input('请输入京东账号:')
jd_pwd = input('请输入京东密码:')
rk_user = input('请输入若快账号:')
rk_pwd = input('请输入若快密码:')
a = JD(jd_user, jd_pwd, rk_user, rk_pwd)
a.login()
a.addcart()
a.submit()