1. 程式人生 > >requests 獲取token

requests 獲取token

turn text get 初始化 upd hone tde sea fir

# encoding:utf-8

import re
import json
import random
from requests.sessions import Session


class Regist(object):
  def __init__(self):
    """初始化session 並設置header和cookie"""

    self.url = "http://xxxx/Account/Regist"
    self.s = Session()
    self.s.headers.setdefault("Content_type", "application/x-www-form-urlencoded")
    self.s.cookies.update(self.get_cookies()[0])

    # self.s.headers.update({"Cookies": self.get_cookies()})

  def get_cookies(self):
    """
    獲取上一次請求的cookie
    :return: 返回cookie dict
    """
    r = self.s.get(self.url)
    rint(r.cookies.get_dict())
    return (r.cookies.get_dict(), r.text)

  def get_token(self):
    ""
    獲取上一次請求隱匿的表單token
    :return:
    """
    pattern = ‘name="__RequestVerificationToken" type="hidden" value="(.*)"‘
    p = re.compile(pattern)
    text = self.get_cookies()[1]
    if p.search(text):
      token = p.search(text).groups()[0]
      token = token.__str__().strip(‘"‘)
    else:
      return
    eturn token

  def get_mobile(self):
    s = "144"
    n = range(10)
    for i in range(8):
    s += random.choice(n).__str__()
    if len(s) == 4 and s == "1442":
    s = (int(s) - 1).__str__()
    print(s)
    return s

  def regist(self):
    param = {"__RequestVerificationToken": self.get_token(),
    "MobilePhone": self.get_mobile(),
    "PassWord": "123456a",
    "ConfirmPassword": "123456a",
    "IntroducerMobile": "",
    "IsAcceptPact": "true"}
    r = self.s.post(self.url, data=param)
    try:
      eturn json.loads(r.text)
    except:
      return r.text


if __name__ == ‘__main__‘:
print(Regist().regist())

requests 獲取token