Last active
November 29, 2018 02:35
-
-
Save ladder1984/2a1231184de59a725f64 to your computer and use it in GitHub Desktop.
自动登录京东网,基于requests、pyquery。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
from pyquery import PyQuery as pq | |
login_url = "https://passport.jd.com/new/login.aspx" | |
login_post_url = "http://passport.jd.com/uc/loginService" | |
# 用户名和密码 | |
username = "your_name" | |
password = "your_password" | |
header_info = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1581.2 Safari/537.36', | |
'Connection': 'keep-alive', | |
} | |
# 创建会话 | |
s = requests.Session() | |
s.headers = header_info | |
login_page_content = s.get(login_url) | |
d = pq(login_page_content.text) | |
# 获得随机数据 | |
uuid = d("#uuid").attr("value") | |
left = d('#formlogin input[type="hidden"]:eq(4)').attr("name") | |
right = d('#formlogin input[type="hidden"]:eq(4)').attr("value") | |
# 获取验证码 | |
auth_img_url = d('#JD_Verification1').attr("src2") | |
img_name = 'authcode.jpg' | |
r = s.get(auth_img_url) | |
with open(img_name, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
f.write(chunk) | |
# 手动填写验证码 | |
authcode = raw_input("please input the authcodde:") | |
authcode = str(authcode) | |
# post数据 | |
login_data = { | |
"loginname": username, | |
"nloginpwd": password, | |
"loginpwd": password, | |
"uuid": uuid, | |
left: right, | |
'authcode': authcode, | |
"chkRememberMe": "on" | |
} | |
x = s.post(login_post_url, data=login_data) | |
print(x.text) | |
# 获得({"success":"http://www.jd.com"}) ,表示登录成功。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment