2 An example tutorial on using the Request library to simulate login in Python-Python Tutorial-php.cn

An example tutorial on using the Request library to simulate login in Python

PHP中文网
Release: 2017-06-20 13:54:08
Original
2132 people have browsed it

Such a simple (unsafe) login form is rare anymore. The login form of the subtitle library is as follows, with irrelevant content omitted:

1 
Copy after login

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
Copy after login

Successful login, analyze the js code in the returned page, and find:

href = document.getElementById('href').href; location.href = href;
Copy after login

Explain that the page you want to jump to is in the hyperlink with the id href:

跳转
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!