2 2
Home > Article > Backend Development > An example tutorial on using the Request library to simulate login in Python
Such a simple (unsafe) login form is rare anymore. The login form of the subtitle library is as follows, with irrelevant content omitted:
1 <form class="login-form" action="/User/login.html" method="post">2 <input type="hidden" name="referer" value="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>
Through packet capture analysis, it can be found that the username and password are not encrypted:
Use POST directly to simulate login:
1 import requests 2 from bs4 import BeautifulSoup 3 4 url='' 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
Successful login, analyze the js code in the returned page, and find:
href = document.getElementById('href').href; location.href = href;
Explain that the page you want to jump to is in the hyperlink with the id href:
<a id="href" href="/User/index.html">跳转</a>
Get the page you want to jump to, and then try to open a new page Whether the login status can be maintained:
1 href=''+bs.find(id='href').attrs['href']2 r2=ss.get(href)3 print(BeautifulSoup(r2.text,'lxml').title.text)#首页 - 用户中心 - 字幕库(zimuku.net)
The words "Home Page - User Center" are printed, and the login status is successfully maintained.
The above is the detailed content of An example tutorial on using the Request library to simulate login in Python. For more information, please follow other related articles on the PHP Chinese website!