您好,登錄后才能下訂單哦!
51cto用python的requests模塊實現(xiàn)登陸示例代碼如下:
-- 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():
#獲取xsrf值
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):
#51cto登錄
post_url = URL
post_data = {
"_csrf": get_xsrf(),
"LoginForm[username]": account,
"LoginForm[password]": password,
#雖然抓包分析時post有以下2個參數(shù),但是實際登陸時以下兩個參數(shù)可有可無
#"LoginForm[rememberMe]": 0,
#"login-button": "登 錄"
}
response = client.post(post_url, data=post_data, headers=header)
#只有上面認(rèn)證通過后,請求以下頁面才可以看到登陸后的結(jié)果
response = client.get(URL)
#print response.content
print response.text
_51cto_login("填入真實的用戶名", "填入真實的密碼")
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。