Python入門:模擬登錄(二)或注冊之requests處理帶token請求


轉自http://blog.csdn.net/foryouslgme/article/details/51822209

首先說一下使用Python模擬登錄或注冊時,對於帶token的頁面怎么登錄注冊模擬的思路: 
1、對於帶token的頁面,需要先從最開始的頁面獲取合法token 
2、然后使用獲取到的合法token進行后續操作 
3、token一般存儲的地方有兩個: 
【一種是攜帶在cookie中】、【一種是在respose(頁面返回)的隱藏表單中】,獲取思路類似 
釋:header是針對服務端有各種限制或特定需求時使用的,一般服務器會進行類似如:X-Requested-With、Content-Length、User-Agent等的驗證,所以需要將其以字典的形勢發送給服務器

 1 #reg.py
 2 '''
 3 此代碼只實現了注冊的第一步【手機發送驗證碼】,主要解決的獲取token
 4 具體操作有:獲取頁面第一次請求時的重要信息,如:cookie與token
 5 '''
 6 import requests
 7 #拼接url
 8 host = "http://10.70.18.33:8083/"
 9 url1 = host + "shopxx-mobile/register.jhtml"
10 #初始化url請求對象
11 r = requests.get(url1)
12 #獲取url請求對象中的有用信息,如token、cookies
13 token = r.cookies.items()[0][1]
14 cookies = r.cookies
15 #以下為測試,所獲取的token及cookie的格式
16 print(type(token))
17 print(token)
18 print(cookies)
19 print(r.headers)
20 print(r.url)
21 #手機號碼發送驗證碼的url拼接
22 url2 = host + "shopxx-mobile/register/send.jhtml"
23 #拼接header中的重要數據,如:token、cookie、User-Agent、Content-Length、X-Requested-With等,其中除token及cookie是通過上面的代碼獲取到的之外,其它的均可以通過firefox瀏覽器進行獲取
24 headers = {
25         "token": token,
26         "Host": "10.70.18.33:8083",
27         "User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0",
28         "Accept": "application/json, text/javascript, */*; q=0.01",
29         "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
30         "Accept-Encoding": "gzip, deflate",
31         "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
32         "X-Requested-With": "XMLHttpRequest",
33         "Connection":"keep-alive",
34         "Content-Length": "18",
35         "charset":"UTF-8",
36         "cookie":"token=" + token
37         }
38 #一般登錄注冊頁面均是post方式進行提交的,需要將post需要提交的數據(此處為需要發送驗證碼的手機號碼)進行組裝
39 data = {'mobile':'1851174****'}
40 #初始化post請求對象(需要傳入url、提交的數據、header)
41 s = requests.post(url2,data,headers=headers)
42 #打印返回結果
43 print(s)
44 print(s.status_code,s.text)

執行結果

/Users/frankslg/PycharmProjects/cjb/mobile/regist.py
<class 'str'>
7c74a4cce353aec3133005feb40a9f39
<RequestsCookieJar[<Cookie token=7c74a4cce353aec3133005feb40a9f39 for 10.70.18.33/>]>
{'Date': 'Mon, 04 Jul 2016 08:53:30 GMT', 'Set-Cookie': 'token=7c74a4cce353aec3133005feb40a9f39; Path=/', 'Server': 'Apache-Coyote/1.1', 'Content-Type': 'text/html;charset=UTF-8', 'Content-Language': 'zh-CN', 'Transfer-Encoding': 'chunked'}
http://10.70.18.33:8083/shopxx-mobile/register.jhtml
<Response [200]>
200 {"type":"success","content":"恭喜您,賬號注冊成功!"}

函數封裝

 

import requests


def reg():
    host = "http://10.70.18.33:8083/"
    url1 = host + "shopxx-mobile/register.jhtml"
    r = requests.get(url1)
    token = r.cookies.items()[0][1]

    url2 = host + "shopxx-mobile/register/send.jhtml"
    headers = {
            "token": token,
            "Host": "10.70.18.33:8083",
            "User-Agent":" Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Language":"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
            "Accept-Encoding": "gzip, deflate",
            "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
            "X-Requested-With": "XMLHttpRequest",
            "Connection":"keep-alive",
            "Content-Length": "18",
            "charset":"UTF-8",
            "cookie":"token=" + token
            }

    data = {'mobile':'1851174****'}
    s = requests.post(url2,data,headers=headers)

    print(s.status_code,s.text)

if __name__ == '__main__':
    reg()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM