import request
try:
import cookielib #python2版本
#except:
import http.cookiejaras cookie
except: import #python3版本import reimport session=request.session()session.cookies=cookielib.LWPCookieJar(filename="cookies. txt") #將cookies儲存到本機檔案#載入cookies檔案try: session.cookies.load(ignore_discard=True) except: print("cookies未能載入")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" : 〦. ,
"User_Agent" : User_Agen"
}
#取得xsrf
def get_xsrf():
[ responsession. post("https://www.zhihu.com",headers=header) #請求網頁需帶上頭檔
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")
##模擬知乎登入
def zhihu_login(account,password):
if re.match("^1\d{10}",account): #驗證帳號是否為手機號
print ("手機登入")
post_url="https://www.zhihu.com/login/phone_num"
## "_xsrf" : get_xsrf(),
"phone_num" : password
}
else:
if "@" in account:
print (「信箱登入」)
### print (「郵箱登入」)###### imail/epost_url_. ### post_data={###### "_xsrf" : xsrf(),#### mail 新 這個:## ##### "password" : password###### } ###### ###### response_text=session.post(post_url,post_data,headers=header)###### session.cookies.save()########## session.cookies.save()##################。 ######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()###### ####以上是request模擬知乎登入實例碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!