Python 模擬登陸的兩種實(shí)現(xiàn)方法
Python 模擬登陸的兩種實(shí)現(xiàn)方法
有時(shí)候我們的抓取項(xiàng)目時(shí)需要登陸到某個(gè)網(wǎng)站上,才能看見(jiàn)某些內(nèi)容的,所以模擬登陸功能就必不可少了,散仙這次寫的文章,主要有2個(gè)例子,一個(gè)是普通寫法寫的,另外一個(gè)是基于面向?qū)ο髮懙摹?/p>
模擬登陸的重點(diǎn),在于找到表單真實(shí)的提交地址,然后攜帶cookie,post數(shù)據(jù)即可,只要登陸成功,我們就可以訪問(wèn)其他任意網(wǎng)頁(yè),從而獲取網(wǎng)頁(yè)內(nèi)容。
方式一:
import urllib.request
import urllib.parse
import http.cookiejar
#post的內(nèi)容
values={
'logon.x':'linke',
'password':'xxxx',
'username':'xxxxx'
}
#登陸的地址
logUrl="http://192.168.32.112:8080/templates/index/hrlogon.do"
#構(gòu)建cook
cook=http.cookiejar.CookieJar()
#構(gòu)建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'))
方式二:
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數(shù)據(jù)
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
#登陸之后獲取其他網(wǎng)頁(yè)方法
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__":
#實(shí)例化類
x=loginRLKQ()
#給post數(shù)據(jù)賦值
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")
#獲取網(wǎng)頁(yè)信息
print(x.getUrlContent("http://192.124.32.16:8080/kq/kqself/card/carddata.do?b_query=link","gbk"))
以上就是Python 模擬登陸的實(shí)現(xiàn)方法,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Python實(shí)現(xiàn)備份MySQL數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)備份MySQL數(shù)據(jù)庫(kù)的方法,涉及Python針對(duì)mysql數(shù)據(jù)庫(kù)的連接及基于mysqldump命令操作數(shù)據(jù)庫(kù)備份的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
Python3.6日志Logging模塊簡(jiǎn)單用法示例
這篇文章主要介紹了Python3.6日志Logging模塊簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Python3.6環(huán)境下日志Logging模塊設(shè)置格式、文件流輸出相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Python pandas入門系列之眾數(shù)和分位數(shù)
分位數(shù)(Quantile),也稱分位點(diǎn),是指將一個(gè)隨機(jī)變量的概率分布范圍分為幾個(gè)等份的數(shù)值點(diǎn),分析其數(shù)據(jù)變量的趨勢(shì),而眾數(shù)(Mode)是代表數(shù)據(jù)的一般水平,這篇文章主要給大家介紹了Python pandas系列之眾數(shù)和分位數(shù)的相關(guān)資料,需要的朋友可以參考下2021-08-08
python正則表達(dá)式re.sub各個(gè)參數(shù)的超詳細(xì)講解
Python 的 re 模塊提供了re.sub用于替換字符串中的匹配項(xiàng),下面這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式re.sub各個(gè)參數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
代碼解析python標(biāo)準(zhǔn)庫(kù)logging模塊
這篇文章主要為大家介紹了代碼解析python標(biāo)準(zhǔn)庫(kù)logging模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

