在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

相关文章
相关标签/搜索