# coding:utf-8
import requests
import json
from lxml import etree
#云打码接口
import ydm
class JD:
def __init__(self,username,password):
self.s = requests.session()
self.username = username
self.password = password
self.headers = {
'Referer':'https://passport.jd.com/new/login.aspx',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}
def get_login_data(self):
url = 'https://passport.jd.com/new/login.aspx'
html = self.s.get(url, headers=self.headers).text
display = etree.HTML(html).xpath('//*[@id="o-authcode"]/@style')
if not display:
print('需要验证码')
auth_code_url = etree.HTML(html).xpath('//*[@id="JD_Verification1"]/@src2')[0]
auth_code = self.get_auth_img(auth_code_url)
else:
auth_code = ''
uuid = etree.HTML(html).xpath('//*[@id="uuid"]/@value')[0]
eid = etree.HTML(html).xpath('//*[@id="eid"]/@value')[0]
fp = etree.HTML(html).xpath('//*[@id="sessionId"]/@value')[0]
_t = etree.HTML(html).xpath('//*[@id="token"]/@value')[0]
loginType = etree.HTML(html).xpath('//*[@id="loginType"]/@value')[0]
loginname = self.username
nloginpwd = self.password
pubKey = etree.HTML(html).xpath('//*[@id="pubKey"]/@value')[0]
sa_token = etree.HTML(html).xpath('//*[@id="sa_token"]/@value')[0]
chkRememberMe = True
data = {
'uuid': uuid,
'eid': eid,
'fp': fp,
'_t': _t,
'loginType': loginType,
'loginname': loginname,
'nloginpwd': nloginpwd,
'pubkey': pubKey,
'sa_token': sa_token,
'authcode': auth_code,
'chkRememberMe': chkRememberMe,
}
return data
def get_auth_img(self, url):
auth_code_url = 'http:' + url
auth_img = self.s.get(auth_code_url, headers=self.headers)
with open('auth.jpg', 'wb') as f:
f.write(auth_img.content)
#云打码
username = '账号'
password = '密码'
appid = 4359
appkey = '0715011dad66b60956de51723d5509c2'
filename = 'auth.jpg'
codetype = 1004
timeout = 60
if (username == 'username'):
print('请设置好相关参数再测试')
else:
yundama = ydm.YDMHttp(username, password, appid, appkey)
balance = yundama.balance()
print('balance: %s' % balance)
# 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
cid, result = yundama.decode(filename, codetype, timeout)
code = result
print(code)
return code
def login(self):
url = 'https://passport.jd.com/uc/loginService'
data = self.get_login_data()
headers = {
'Referer': 'https://passport.jd.com/uc/login?ltype=logout',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0',
'X-Requested-With': 'XMLHttpRequest'
}
content = self.s.post(url,data=data,headers=headers).text
result = json.loads(content[1: -1])
print(result)
return result
j = JD('username','password')
res = j.login()
if res.get('success'):
print('登录成功啦')
else:
j.login()