Python模擬登錄和登錄跳轉(zhuǎn)的參考示例
更新時間:2020年10月30日 10:02:34 作者:py3study
這篇文章主要介紹了Python模擬登錄和登錄跳轉(zhuǎn)的參考示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
# coding:utf-8 import urllib import urllib2 import cookielib from bs4 import BeautifulSoup # 設(shè)置登錄url login_url = "******************" # 創(chuàng)建登錄類 class Login(object): #初始化 def __init__(self): self.username = '' self.password = '' # 驗證碼 self.rode = '' #設(shè)置cookie self.cj = cookirlib,LWPCookieJar() self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) urllib2.install_opener(self.opener) def setLoginInfo(self, username, passwoed ,rode): """設(shè)置登錄用戶信息""" self.username = username self.password = password self.rode = rode def login(self): """"模擬登錄""" login_data = {'username': self.username, 'password': self.password, 'rode': self.rode} headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2859.0 Safari/537.36'} req = urllib2.Request(login_url, data=urllib.urlencode(login_data), headers=headers) response = urllibs.urlopen(req) # 加cookie, 自動保存seissionid,做后續(xù)跳轉(zhuǎn) self.operate = self.opener.open(req) # 通過BeautifulSoup獲取網(wǎng)頁內(nèi)容 soup = BeautifulSoup(response, 'lxml') # 獲取網(wǎng)頁的content data = soup.select('#content') if data: print u'模擬登錄成功!', data print u'模擬登錄失敗!' def skip(self, skip_url): """ 模擬登錄成功后,跳轉(zhuǎn)網(wǎng)頁 ---------------------------- 傳遞跳轉(zhuǎn)網(wǎng)頁的url """ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2859.0 Safari/537.36'} req = urllib2.Request(skip_url, headers=headers) response = urllib2.urlopen(req) soup = BeautifulSoup(response, 'lxml') # 獲取跳轉(zhuǎn)后網(wǎng)頁的title print soup.title.string if __name__ == "__main__": userlogin = Login() username = '***********' password = '***********' rode = '***********' # 執(zhí)行模擬登錄方法 userlogin.login() # 執(zhí)行模擬登錄成功后網(wǎng)頁跳轉(zhuǎn)方法 user_login.skip(skip_url = '***************')
以上就是Python模擬登錄和登錄跳轉(zhuǎn)的參考示例的詳細內(nèi)容,更多關(guān)于Python模擬登錄和登錄跳轉(zhuǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
相關(guān)文章
Python計算開方、立方、圓周率,精確到小數(shù)點后任意位的方法
今天小編就為大家分享一篇Python計算開方、立方、圓周率,精確到小數(shù)點后任意位的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python利用柯里化實現(xiàn)提高代碼質(zhì)量
柯里化(Currying)是函數(shù)式編程中的一個重要概念,它可以將一個多參數(shù)函數(shù)轉(zhuǎn)化為一系列單參數(shù)函數(shù)的組合,本文將詳細解釋什么是柯里化,如何在Python中實現(xiàn)柯里化,感興趣的可以了解下2024-01-01使用Python代碼實現(xiàn)Linux中的ls遍歷目錄命令的實例代碼
這次我就要試著用 Python 來實現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡單,需要的朋友可以參考下2019-09-09Python3使用tesserocr識別字母數(shù)字驗證碼的實現(xiàn)
這篇文章主要介紹了Python3使用tesserocr識別字母數(shù)字驗證碼的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01常用python數(shù)據(jù)類型轉(zhuǎn)換函數(shù)總結(jié)
這篇文章主要介紹了常用的python數(shù)據(jù)類型轉(zhuǎn)換函數(shù),并用實際例子說明了這些函數(shù)的用法,需要的朋友可以參考下2014-03-03