Python模擬登錄和登錄跳轉(zhuǎn)的參考示例
# coding:utf-8import urllibimport urllib2import cookielibfrom bs4 import BeautifulSoup# 設(shè)置登錄urllogin_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.stringif __name__ == '__main__': userlogin = Login() username = ’***********’ password = ’***********’ rode = ’***********’ # 執(zhí)行模擬登錄方法 userlogin.login() # 執(zhí)行模擬登錄成功后網(wǎng)頁跳轉(zhuǎn)方法 user_login.skip(skip_url = ’***************’)
以上就是Python模擬登錄和登錄跳轉(zhuǎn)的參考示例的詳細(xì)內(nèi)容,更多關(guān)于Python模擬登錄和登錄跳轉(zhuǎn)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章: