十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Python实现模拟登录和登录跳转的方法?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联公司科技有限公司专业互联网基础服务商,为您提供服务器托管,高防服务器租用,成都IDC机房托管,成都主机托管等互联网服务。# coding:utf-8 import urllib import urllib2 import cookielib from bs4 import BeautifulSoup # 设置登录url login_url = "******************" # 创建登录类 class Login(object): #初始化 def __init__(self): self.username = '' self.password = '' # 验证码 self.rode = '' #设置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): """设置登录用户信息""" 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,做后续跳转 self.operate = self.opener.open(req) # 通过BeautifulSoup获取网页内容 soup = BeautifulSoup(response, 'lxml') # 获取网页的content data = soup.select('#content') if data: print u'模拟登录成功!', data print u'模拟登录失败!' def skip(self, skip_url): """ 模拟登录成功后,跳转网页 ---------------------------- 传递跳转网页的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') # 获取跳转后网页的title print soup.title.string if __name__ == "__main__": userlogin = Login() username = '***********' password = '***********' rode = '***********' # 执行模拟登录方法 userlogin.login() # 执行模拟登录成功后网页跳转方法 user_login.skip(skip_url = '***************')