Login example of python's requests module implementation
51cto uses python's requests module to implement the login example code as follows:
-- coding:utf-8--
Import requests
Import re
Client = requests.session ()
Agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
Header = {
"User-Agent": agent
}
URL = "http://home.51cto.com/index"
Def get_xsrf ():
# get xsrf value
Response = client.get (URL, headers=header)
Match_obj = re.search ('.name = "csrf-token" content= "(.)"', response.text)
If match_obj:
Return match_obj.group (1)
Else:
Return ""
Def _ 51cto_login (account, password):
# logging in to 51cto
Post_url = URL
Post_data = {
"_ csrf": get_xsrf ()
"LoginForm [username]": account
"LoginForm [password]": password
# although post has the following two parameters during packet capture analysis, the following two parameters are optional when actually logging in
# "LoginForm [remember me]": 0
# "login-button": "log in"
}
Response = client.post (post_url, data=post_data, headers=header)
# only after the above authentication is passed, can you see the login result by requesting the following page
Response = client.get (URL)
# print response.content
Print response.text
_ 51cto_login ("fill in real user name", "fill in real password")