Home  >  Article  >  Backend Development  >  request simulates Zhihu login example code

request simulates Zhihu login example code

零下一度
零下一度Original
2017-06-23 10:43:171491browse

import request

try:

import cookielib #python2 version

except:

import http.cookiejar as cookielib #python3 version

import re

import

session=request.session()

session.cookies=cookielib.LWPCookieJar(filename="cookies. txt") #Store cookies to local files

#Load cookies files

try:

session.cookies.load(ignore_discard=True)

except:

 print("cookies failed to load")

User_Agent="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/ 537.36"

header={

 "HOST"  : "www.zhihu.com",

 "Referer"    : "https://www.zhihu.com" ,

 "User_Agent"  : User_Agen"

}

#Get xsrf

def get_xsrf():

 response=session. post("https://www.zhihu.com",headers=header) #Requesting web pages must bring header files

match_obj=re.match(' .*name="_xsrf" value="(. " "

def get_index():

 response=session.get("https://www.zhihu.com",headers=header)

 with open(" index_page.heml",wb) as f:

 f,write(response.text.encode("utf-8"))

print ("ok")

#Simulate Zhihu login

def zhihu_login(account,password):

if re.match("^1\d{10}",account): #Verify whether the account is a mobile phone No.

  print ("Mobile phone login")

  post_url="https://www.zhihu.com/login/phone_num"

  post_data={

   "_xsrf"  :get_xsrf(),

   "phone_num"   :account,

   "password"   :password

   }

 else:

 if "@" in account:

  print ("Email login")

  post_url="https://www.zhihu.com/login/email"

Post_data={

response_text=session.post(post_url,post_data,headers=header)

session.cookies.save()

#Verify whether the login is successful

def is_login():

inbox_url="https://www.zhihu.com/inbox"

response=session.get(inbox_url,headers=header, allow_redirects=False)

if response.status_code !=200:

return False

else:

return True

zhihu. login("18782902568","admin123")

get_index()

The above is the detailed content of request simulates Zhihu login example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn