在Python中用Request庫模擬登陸(一):字幕庫(無加密,無驗證碼)

字幕庫的登陸表單以下所示,其中省去了可有可無的內容:html

1 <form class="login-form" action="/User/login.html" method="post">
2     <input type="hidden" name="referer" value="http://www.zimuku.net/">
3     <input type="text" id="inputEmail" datatype="*1-16" value="" name="username">
4     <input type="password" id="inputPassword" datatype="*6-20" name="password">
5     <input type="checkbox" name="isremember" value="1" checked="">
6     <button type="submit" class="btn submit-btn">登 陸</button>
7 </form>  

經過抓包分析,能夠發現用戶名和密碼都沒有被加密:session

直接使用POST來模擬登陸:post

 1 import requests
 2 from bs4 import BeautifulSoup
 3 
 4 url='http://www.zimuku.net/User/login.html'
 5 data={'referer':'','username':'***','password':'***','isremember':'1'}
 6 
 7 #建立會話
 8 session=requests.session()
 9 #模擬登陸
10 r=session.post(url,data=data)
11 #解析頁面
12 bs=BeautifulSoup(r.text,'lxml')
13 
14 print(bs.body.text) #登陸成功!頁面自動 跳轉 等待時間: 1

成功登陸,分析返回頁面中的js代碼,發現有:加密

href = document.getElementById('href').href;
location.href = href;

說明要跳轉到的頁面在id爲href的超連接中:url

<a id="href" href="/User/index.html">跳轉</a>

獲取要跳轉到的頁面,而後嘗試打開新頁面時登陸狀態可否被保持:spa

1 href='http://www.zimuku.net'+bs.find(id='href').attrs['href']
2 r2=ss.get(href)
3 print(BeautifulSoup(r2.text,'lxml').title.text)#首頁 - 用戶中心 - 字幕庫(zimuku.net)

打印出了「首頁 - 用戶中心」字樣,成功保持登陸狀態。.net

相關文章
相關標籤/搜索