


Code examples of two methods to implement simulated login in Python
This article mainly introduces relevant information about two implementation methods of simulated login in Python. Here are two methods. One is written in ordinary writing, and the other is written based on object-oriented. The crawling can only be done after the simulated login is successful. Content, friends who need it can refer to
Two implementation methods of Python simulated login
Sometimes we need to log in to a certain website when crawling projects. Only in order to see certain content, the simulated login function is indispensable. The article written by Sanxian this time mainly has two examples, one is written in ordinary writing method, and the other is based on object-oriented writing.
The focus of simulated login is to find the real submission address of the form, and then carry the cookie and post data. As long as the login is successful, we can access any other web page to obtain the web page content.
Method one:
import urllib.request import urllib.parse import http.cookiejar #post的内容 values={ 'logon.x':'linke', 'password':'xxxx', 'username':'xxxxx' } #登陆的地址 logUrl="http://192.168.32.112:8080/templates/index/hrlogon.do" #构建cook cook=http.cookiejar.CookieJar() #构建openner openner=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cook)) #添加headers openner.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')] r=openner.open(logUrl,urllib.parse.urlencode(values).encode()) #print(r.read().decode('gbk')) r=openner.open("http://192.168.132.62:8080/kq/kqself/card/carddata.do?b_query=link") print(r.read().decode('gbk'))
Method two:
import urllib import urllib.request import urllib.parse import http.cookiejar import re class loginRLKQ: post_data=b""; def __init__(self): #初始化类,cook的值 cj=http.cookiejar.CookieJar() opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) opener.addheaders=[('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')] #初始化全局opener urllib.request.install_opener(opener) #login方法需要加入post数据 def login(self,loginurl,encode): #模拟登陆 req=urllib.request.Request(loginurl,self.post_data) rep=urllib.request.urlopen(req) d=rep.read() #print(d) d=d.decode(encode) return d #登陆之后获取其他网页方法 def getUrlContent(self,url,encode): req2=urllib.request.Request(url) rep2=urllib.request.urlopen(req2) d2=rep2.read() d22=d2.decode(encode) return d22 if __name__=="__main__": #实例化类 x=loginRLKQ() #给post数据赋值 x.post_data=urllib.parse.urlencode({'username':"xxdd",'password':'xxdd','logon.x':'linke'}).encode(encoding="gbk") #登陆 y=x.login("http://192.168.132.61:8080/templates/index/hrlogon.do","gbk") #获取网页信息 print(x.getUrlContent("http://192.124.32.16:8080/kq/kqself/card/carddata.do?b_query=link","gbk"))
The above is the detailed content of Code examples of two methods to implement simulated login in Python. For more information, please follow other related articles on the PHP Chinese website!

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft