字幕库的登录表单如下所示,其中省去了无关紧要的内容:
<form class="login-form" action="/User/login.html" method="post"> <input type="hidden" name="referer" value="http://www.zimuku.net/"> <input type="text" id="inputEmail" datatype="*1-16" value="" name="username"> <input type="password" id="inputPassword" datatype="*6-20" name="password"> <input type="checkbox" name="isremember" value="1" checked=""> <button type="submit" class="btn submit-btn">登 陆</button> </form>
通过抓包分析,可以发现用户名和密码都没有被加密:
直接使用POST来模拟登录:
import requests from bs4 import BeautifulSoup url='http://www.zimuku.net/User/login.html' data={'} #创建会话 session=requests.session() #模拟登录 r=session.post(url,data=data) #解析页面 bs=BeautifulSoup(r.text,'lxml') print(bs.body.text) #登录成功!页面自动 跳转 等待时间: 1
成功登录,分析返回页面中的js代码,发现有:
href = document.getElementById('href').href; location.href = href;
说明要跳转到的页面在id为href的超链接中:
<a id="href" href="/User/index.html">跳转</a>
获取要跳转到的页面,然后尝试打开新页面时登录状态能否被保持:
href='http://www.zimuku.net'+bs.find(id='href').attrs['href'] r2=ss.get(href) print(BeautifulSoup(r2.text,'lxml').title.text)#首页 - 用户中心 - 字幕库(zimuku.net)
打印出了“首页 - 用户中心”字样,成功保持登录状态。