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!