# Coding:utf-8
ImportRequests
ImportRe
fromBs4ImportBeautifulSoup
# s= Requests.session () # Global S
defGet_token(S):
""
Fuction:Get Token
Args:s parameter-"s = requests.session ()
: Returnanti_token->{' x-anit-forge-token ': ' xx ', ' x-anit-forge-code ': ' 38515842 '}
‘‘‘
# local s not defined, incoming s from outside
Url= ' https://passport.lagou.com/login/login.html '
H1= {
"User-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 "
}
R1= S.get(URL, headers=H1, verify=False)
# Print (R1.text)
Soup= BeautifulSoup(R1.content,"Html.parser", from_encoding=' Utf-8 ')
Tokencode= {}
Try:
T= Soup.find_all(' script ')[1]. get_text()
Print(T)
Tokencode[' X_anti_forge_token '] = Re.findall(r "Token = ' (. +?) '"+ S)[0]
Tokencode[' X_anti_forge_code '] = Re.findall(r "Code = ' (. +?) '"+ S)[0]
returnTokencode
except:
Print("get token and code failed ")
Tokencode[' X_anti_forge_token '] = ""
Tokencode[' X_anti_forge_code '] = ""
returnTokencode
defLOGIN_LGW(S, Anti_token, user, PSW):
Url2= ' Https://passport.lagou.com/login/login.json '
H2= {
"User-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 ",
"Content-type": "Application/x-www-form-urlencoded;charset=utf-8",
"X-requested-with": "XMLHttpRequest",
"X-anit-forge-token": Anti_token[' X_anti_forge_token '] ,
"X-anit-forge-code": Anti_token[' X_anti_forge_code '],
"Referer": "Https://passport.lagou.com/login/login.html"
}
Body={
"Isvalidate": "true",
"username": User
"Password": Psw
"Request_form_verifycode": "",
"Submit": ""
}
Print(S.headers) # s of the head
# Update the head of S
S.headers.update(H2)
Print(S.headers)
R2= S.post(URL2, data=Body, verify=False)
Print(R2.text)
returnR2.json()
if__name__== "__main__":
# Self-Test content
S= Requests.session()
Token= Get_token(S)
Print(Token)
User= "1232"
Psw= "322222"
Login_lgw(s, token, user, PSW)
# function
A= [1, 3, 6, 4, 85, 32, 46]
Print(Sum(A)) # Sum, Sum function
defAdd():
A= 1,
B= 2,
returnA+ B
Print(Add())
defAdd(A, b): # all must be filled
returnA+ B
Print(Add())
defAdd(A=0, B=0): # are not required
returnA+ B
Print(Add())
defAdd(A, b=0): # A is required (required fields are placed in front)
returnA+ B
Print(Add(1, 3)) # do not specify parameter names, in order to pass parameters
Print(Add(A=1, b=3)) # Specify the parameter name and pass the parameter by corresponding parameters
defAdd(A=0, B=0,*Args): # *args variable parameters (variable number of parameters, unlimited)
returnA+ B
Print(Add())
# def add (A, B):
# # understood as an interface
# return A+b
# # Enter the parameter and the API interface
# Print (Add (1, 2))
# Classes and methods
classCount():
def__init__(Self, AAA, BBB): # initialization
# Public parameters can be put in
Print('when instantiated, the contents of init are executed ')
Self.a= Aaa
self.b= Bbb
# Add Self is a global variable, no self is a local variable
defABB(Self): # The calling method inside the class is not called a function
returnSelf.a+ self.b
defABB(Self, A, b): # The calling method inside the class is not called a function
returnSelf.a* self.b
defAEE(Self, A, b):
# Self is the object itself, instance parameters of the class itself
Self.abb() # internal call when directly self. method
off__name__==' __main__ ': # The following code runs only in the current script
Count= Count() # Instantiate a class as an object, no arguments at this time, and the methods inside the class can be invoked after instantiation
Count.add(1, 3) # Call the Add method of the A object
B= Count(10, 5)
Aa= B.abb()
Print(Aa)
# coding:utf-8
Import requests
Header = { # login Grab Packet Get header
"user-agent": "mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) gecko/20100101 firefox/44.0 ",
" Accept ":" * /* ",
" 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 ",
" content-length ":" 423 " ,
"Connection": "Keep-alive"
}
Body= {"Key1": "Value1 ",
"Key2": "Value2"}# The account code here is the packet capture data
S= Requests.session()
Login_url= "Http://xxx.login"# Find your own token URL
Login_ret= S.post(Login_url, headers=Header, data=Body)
# here token in the returned JSON, can be extracted directly
Token= Login_ret.json() ["token"] #take it out first.
# This is a POST request sent after login
Post_url= "Http://xxx"
# Add token to request header
Header["token"] =Token # not always in the head, for reference only
# If the other parameters of the header of the POST request change, you can also update directly
Header["Content-length"]= "9"
Body1= {
"Key": "Value"
}
S.post(post_url, headers=header, data=body1)
Post_ret.content
Python interface Automation Test VI: Classes and methods