Multiple methods of Python simulated login
This article mainly introduces various methods of Python simulated login. It provides you with roughly four methods. Each method is introduced to you in detail. Friends who are interested should take a look.
Text
Method 1: Directly use known cookies to access
Features:
Simple, but you need to log in to the browser first
Principle:
Simply put, cookies are stored in the client that initiates the request, and the server uses cookies to distinguish between different client. Because HTTP is a stateless connection, when the server receives several requests at once, it cannot determine which requests were initiated by the same client. The behavior of "accessing pages that can only be seen after logging in" requires the client to prove to the server: "I am the client who just logged in." So a cookie is needed to identify the client and store its information (such as login status).
Of course, this also means that as long as we get the cookie of another client, we can pretend to be it to talk to the server. This gives our program an opportunity.
We first log in with a browser, and then use developer tools to view cookies. Then, by carrying the cookie in the program and sending a request to the website, your program can pretend to be the browser you just logged in and get the page that can only be seen after logging in.
Specific steps:
1. Log in with a browser and get the cookie string in the browser
Use first Browser login. Open the developer tools again and go to the network tab. Find the current URL in the Name column on the left, select the Headers tab on the right, and view the Request Headers, which contains the cookies issued to the browser by the website. Yes, it's the string behind it. Copy it, we will use it in the code later.
Note, it is best to log in before running your program. If you log in too early or close the browser, it is likely that the copied cookie will expire.
2. Write code
Version of urllib library:
import sys import io from urllib import request sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码 #登录后才能访问的网站 url = 'http://ssfw.xmu.edu.cn/cmstar/index.portal' #浏览器登录后得到的cookie,也就是刚才复制的字符串 cookie_str = r'JSESSIONID=xxxxxxxxxxxxxxxxxxxxxx; iPlanetDirectoryPro=xxxxxxxxxxxxxxxxxx' #登录后才能访问的网页 url = 'http://ssfw.xmu.edu.cn/cmstar/index.portal' req = request.Request(url) #设置cookie req.add_header('cookie', raw_cookies) #设置请求头 req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36') resp = request.urlopen(req) print(resp.read().decode('utf-8'))
requests Library version:
import requests import sys import io sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码 #登录后才能访问的网页 url = 'http://ssfw.xmu.edu.cn/cmstar/index.portal' #浏览器登录后得到的cookie,也就是刚才复制的字符串 cookie_str = r'JSESSIONID=xxxxxxxxxxxxxxxxxxxxxx; iPlanetDirectoryPro=xxxxxxxxxxxxxxxxxx' #把cookie字符串处理成字典,以便接下来使用 cookies = {} for line in cookie_str.split(';'): key, value = line.split('=', 1) cookies[key] = value
Method 2: Simulate login and then bring the obtained cookie to access
Principle:
We first send a login request to the website in the program, that is, submit a form containing login information (user name, password, etc.). Obtain the cookie from the response and bring this cookie with you when visiting other pages in the future, so that you can get the page that can only be seen after logging in.
Specific steps:
1. Find out the page to which the form is submitted
You still need to use the browser developer tool. Go to the network tab and check Preserve Log (important!). Log in to the website in your browser. Then find the page to which the form was submitted in the Name column on the left. How to find it? Look to the right and go to the Headers tab. First of all, in the General section, the Request Method should be POST. Secondly, there should be a section called Form Data at the bottom, where you can see the user name and password you just entered. You can also look at the Name on the left. If it contains the word login, it may be the page where the form is submitted (not necessarily!).
It should be emphasized here that the "page to which the form is submitted" is usually not the page where you fill in your username and password! So use the tools to find it.
2. Find out the data to be submitted
Although you only filled in your username and password when logging in in the browser, the form contains more than that. You can see all the data that needs to be submitted from Form Data.
3. Write code
Version of urllib library:
import sys import io import urllib.request import http.cookiejar sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码 #登录时需要POST的数据 data = {'Login.Token1':'学号', 'Login.Token2':'密码', 'goto:http':'//ssfw.xmu.edu.cn/cmstar/loginSuccess.portal', 'gotoOnFail:http':'//ssfw.xmu.edu.cn/cmstar/loginFailure.portal'} post_data = urllib.parse.urlencode(data).encode('utf-8') #设置请求头 headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'} #登录时表单提交到的地址(用开发者工具可以看到) login_url = ' http://ssfw.xmu.edu.cn/cmstar/userPasswordValidate.portal #构造登录请求 req = urllib.request.Request(login_url, headers = headers, data = post_data) #构造cookie cookie = http.cookiejar.CookieJar() #由cookie构造opener opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)) #发送登录请求,此后这个opener就携带了cookie,以证明自己登录过 resp = opener.open(req) #登录后才能访问的网页 url = 'http://ssfw.xmu.edu.cn/cmstar/index.portal' #构造访问请求 req = urllib.request.Request(url, headers = headers) resp = opener.open(req) print(resp.read().decode('utf-8'))
requests Library version:
import requests import sys import io sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码 #登录后才能访问的网页 url = 'http://ssfw.xmu.edu.cn/cmstar/index.portal' #浏览器登录后得到的cookie,也就是刚才复制的字符串 cookie_str = r'JSESSIONID=xxxxxxxxxxxxxxxxxxxxxx; iPlanetDirectoryPro=xxxxxxxxxxxxxxxxxx' #把cookie字符串处理成字典,以便接下来使用 cookies = {} for line in cookie_str.split(';'): key, value = line.split('=', 1) cookies[key] = value #设置请求头 headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'} #在发送get请求时带上请求头和cookies resp = requests.get(url, headers = headers, cookies = cookies) print(resp.content.decode('utf-8'))
Obviously it feels more convenient to use the requests library~~~
Method three: Use session to maintain login status after simulating login
Principle:
Session means session. Similar to cookies, it also allows the server to "recognize" the client. The simple understanding is to treat every interaction between the client and the server as a "session". Since they are in the same "session", the server will naturally know whether the client has logged in.
Specific steps:
1. Find out the page to which the form is submitted
2. Find out the data to be submitted
这两步和方法二的前两步是一样的
3.写代码
requests库的版本
import requests import sys import io sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改变标准输出的默认编码 #登录时需要POST的数据 data = {'Login.Token1':'学号', 'Login.Token2':'密码', 'goto:http':'//ssfw.xmu.edu.cn/cmstar/loginSuccess.portal', 'gotoOnFail:http':'//ssfw.xmu.edu.cn/cmstar/loginFailure.portal'} #设置请求头 headers = {'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'} #登录时表单提交到的地址(用开发者工具可以看到) login_url = 'http://ssfw.xmu.edu.cn/cmstar/userPasswordValidate.portal' #构造Session session = requests.Session() #在session中发送登录请求,此后这个session里就存储了cookie #可以用print(session.cookies.get_dict())查看 resp = session.post(login_url, data) #登录后才能访问的网页 url = 'http://ssfw.xmu.edu.cn/cmstar/index.portal' #发送访问请求 resp = session.get(url) print(resp.content.decode('utf-8'))
方法四:使用无头浏览器访问
特点:
功能强大,几乎可以对付任何网页,但会导致代码效率低
原理:
如果能在程序里调用一个浏览器来访问网站,那么像登录这样的操作就轻而易举了。在Python中可以使用Selenium库来调用浏览器,写在代码里的操作(打开网页、点击……)会变成浏览器忠实地执行。这个被控制的浏览器可以是Firefox,Chrome等,但最常用的还是PhantomJS这个无头(没有界面)浏览器。也就是说,只要把填写用户名密码、点击“登录”按钮、打开另一个网页等操作写到程序中,PhamtomJS就能确确实实地让你登录上去,并把响应返回给你。
具体步骤:
1.安装selenium库、PhantomJS浏览器
2.在源代码中找到登录时的输入文本框、按钮这些元素
因为要在无头浏览器中进行操作,所以就要先找到输入框,才能输入信息。找到登录按钮,才能点击它。
在浏览器中打开填写用户名密码的页面,将光标移动到输入用户名的文本框,右键,选择“审查元素”,就可以在右边的网页源代码中看到文本框是哪个元素。同理,可以在源代码中找到输入密码的文本框、登录按钮。
3.考虑如何在程序中找到上述元素
Selenium库提供了find_element(s)_by_xxx的方法来找到网页中的输入框、按钮等元素。其中xxx可以是id、name、tag_name(标签名)、class_name(class),也可以是xpath(xpath表达式)等等。当然还是要具体分析网页源代码。
4.写代码
import requests import sys import io from selenium import webdriver sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8') #改变标准输出的默认编码 #建立Phantomjs浏览器对象,括号里是phantomjs.exe在你的电脑上的路径 browser = webdriver.PhantomJS('d:/tool/07-net/phantomjs-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe') #登录页面 url = r'http://ssfw.xmu.edu.cn/cmstar/index.portal' # 访问登录页面 browser.get(url) # 等待一定时间,让js脚本加载完毕 browser.implicitly_wait(3) #输入用户名 username = browser.find_element_by_name('user') username.send_keys('学号') #输入密码 password = browser.find_element_by_name('pwd') password.send_keys('密码') #选择“学生”单选按钮 student = browser.find_element_by_xpath('//input[@value="student"]') student.click() #点击“登录”按钮 login_button = browser.find_element_by_name('btn') login_button.submit() #网页截图 browser.save_screenshot('picture1.png') #打印网页源代码 print(browser.page_source.encode('utf-8').decode()) browser.quit()
相关推荐:
The above is the detailed content of Multiple methods of Python simulated login. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

MySQL download file is corrupt, what should I do? Alas, if you download MySQL, you can encounter file corruption. It’s really not easy these days! This article will talk about how to solve this problem so that everyone can avoid detours. After reading it, you can not only repair the damaged MySQL installation package, but also have a deeper understanding of the download and installation process to avoid getting stuck in the future. Let’s first talk about why downloading files is damaged. There are many reasons for this. Network problems are the culprit. Interruption in the download process and instability in the network may lead to file corruption. There is also the problem with the download source itself. The server file itself is broken, and of course it is also broken when you download it. In addition, excessive "passionate" scanning of some antivirus software may also cause file corruption. Diagnostic problem: Determine if the file is really corrupt

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

MySQL refused to start? Don’t panic, let’s check it out! Many friends found that the service could not be started after installing MySQL, and they were so anxious! Don’t worry, this article will take you to deal with it calmly and find out the mastermind behind it! After reading it, you can not only solve this problem, but also improve your understanding of MySQL services and your ideas for troubleshooting problems, and become a more powerful database administrator! The MySQL service failed to start, and there are many reasons, ranging from simple configuration errors to complex system problems. Let’s start with the most common aspects. Basic knowledge: A brief description of the service startup process MySQL service startup. Simply put, the operating system loads MySQL-related files and then starts the MySQL daemon. This involves configuration

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

MySQL can run without network connections for basic data storage and management. However, network connection is required for interaction with other systems, remote access, or using advanced features such as replication and clustering. Additionally, security measures (such as firewalls), performance optimization (choose the right network connection), and data backup are critical to connecting to the Internet.
