參考鏈接: 從Python獲取輸入
Python京東搶購?
分析其中提交信息接口的參數,可以成功搶購商品,并且可以提交訂單。。。。2018年7月17日?
提交信息的獲取?
直接提交信息對post提交分析其中的參數。? 經過分析參數大多數在:https://passport.jd.com/new/login.aspx??
圖片驗證碼地址?
判斷登陸是否有驗證碼:后面的/uc/showAuthCode可以判斷是一個url。? 訪問上面的code_url地址:可以看到獲取的是false不需要驗證碼,true需要驗證碼? 分析驗證碼地址的url:? 分析驗證碼的地址:驗證碼是顯示在login.aspx嘗試是否能在里面獲取參數信息。可以看到里面含有image_url圖片后面的參數??
進行post信息提交?
分析post提交url的地址:登陸成功回返回success信息??
接下來獲取用戶的信息:? ?就以上三步分析,主要還是其中參數分析比較困難,以及提交url地址后面需要的參數。有的時候url地址后面不要參數可以,但是有的時候沒參數就不行。post提交地址的時候,一開始沒有加上uuid參數就一直不能提交成功。?
加入購物車并提交訂單?
添加到購物車的接口? 注意這里提交訂單需要加上header頭中的’referer’: ‘https://cart.jd.com/cart.action’。? 接下來就是提交訂單的參數??
貼上搶購過程?
? 可以看到訂單號是相同的,物品購買成功? 加入了打碼模塊,和添加多個物品去購物車,以及商品倒計時。??
完整登陸代碼?
import requests
import json
import time
import json
from pyquery import PyQuery as pq
from pprint import pprint as pp
?
?
class JingDong:
? ? headers = {
? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36'
? ? ? ? ' (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
? ? ? ? 'Referer': 'https://www.jd.com/',
? ? }
?
? ? def __init__(self, username, password):
? ? ? ? self.index_url = 'https://passport.jd.com/new/login.aspx'? # 京東首頁的地址
? ? ? ? self.auth_url = 'https://passport.jd.com/uc/showAuthCode'? # 判斷驗證碼地址
? ? ? ? self.post_url = 'https://passport.jd.com/uc/loginService?uuid{}<ype=logout&version=2015'? # 登陸的地址
? ? ? ? self.user_url = 'https://passport.jd.com/user/petName/getUserInfoForMiniJd.action?&callback=jsonpUserinfo&_=' + \
? ? ? ? ? ? str(int(time.time() * 1000))? # 檢測用戶信息
? ? ? ? self.session = requests.Session()? # session通信
? ? ? ? self.username = username
? ? ? ? self.password = password
? ? ? ? self.uuid = ''
?
? ? def login_info(self):? # 獲取登陸信息的參數
? ? ? ? response = self.session.get(
? ? ? ? ? ? url=self.index_url, headers=self.headers).text
? ? ? ? doc = pq(response)
? ? ? ? sa_token = doc('#sa_token').attr('value')
? ? ? ? uuid = doc('#uuid').attr('value')
? ? ? ? self.uuid = uuid
? ? ? ? eid = doc('#eid').attr('value')
? ? ? ? fp = doc('#sessionId').attr('value')
? ? ? ? _t = doc('#token').attr('value')
? ? ? ? loginType = doc('#loginType').attr('value')
? ? ? ? pubKey = doc('#pubKey').attr('value')
?
? ? ? ? response = self.session.get(
? ? ? ? ? ? url=self.auth_url, headers=self.headers).text
? ? ? ? if 'true' in response:? # 返回的true就是需要驗證碼
? ? ? ? ? ? auth_code_url = doc('#JD_Verification1').attr('src2')? # 獲取code的url
? ? ? ? ? ? auth_code = str(self.get_code(auth_code_url))
? ? ? ? else:
? ? ? ? ? ? auth_code = ''
? ? ? ? data = {? # 提交的參數
? ? ? ? ? ? 'uuid': uuid,
? ? ? ? ? ? 'eid': eid,
? ? ? ? ? ? 'fp': fp,
? ? ? ? ? ? '_t': _t,
? ? ? ? ? ? 'loginType': loginType,
? ? ? ? ? ? 'loginname': self.username,
? ? ? ? ? ? 'nloginpwd': self.password,
? ? ? ? ? ? 'authcode': auth_code,
? ? ? ? ? ? 'pubKey': pubKey,
? ? ? ? ? ? 'sa_token': sa_token
? ? ? ? }
? ? ? ? return data
?
? ? def get_code(self, url):? # 獲取驗證碼
? ? ? ? time_str = str((int)(time.time() * 1000))
? ? ? ? code_url = f'https:{url}&yys={time_str}'? # 拼接code_img的地址
? ? ? ? response = self.session.get(
? ? ? ? ? ? url=code_url, headers=self.headers, stream=True)
? ? ? ? with open('code.jpg', 'wb') as f:
? ? ? ? ? ? for chunk in response.iter_content(1024):
? ? ? ? ? ? ? ? f.write(chunk)
? ? ? ? code_text = input('請輸入驗證碼:')
? ? ? ? return code_text
?
? ? def login(self):
? ? ? ? data = self.login_info()? # 獲取提交的參數
? ? ? ? headers = {
? ? ? ? ? ? 'Referer': self.post_url,
? ? ? ? ? ? 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36'
? ? ? ? ? ? ? ? ? ? ? ? ? ' (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
? ? ? ? ? ? 'X-Requested-With': 'XMLHttpRequest'
? ? ? ? }
? ? ? ? try:
? ? ? ? ? ? url = self.post_url.format(self.uuid)? # 拼接提交url
? ? ? ? ? ? login_page = self.session.post(
? ? ? ? ? ? ? ? url, data=data, headers=headers)? ? ? ? # 提交登陸
? ? ? ? ? ? if 'success' in login_page.text:
? ? ? ? ? ? ? ? print('登陸成功')
? ? ? ? ? ? response = self.session.get(
? ? ? ? ? ? ? ? url=self.user_url, headers=headers)? ? ? ? # 獲取登陸信息
? ? ? ? ? ? response = response.text.strip('jsonpUserinfo()\n')
? ? ? ? ? ? pp(json.loads(response))
? ? ? ? except Exception as e:
? ? ? ? ? ? raise e
jingdong = JingDong('用戶名', '密碼')
jingdong.login()