python2实现模拟登陆UESTC信息门户

手动登陆查看post的数据包信息

打开UESTC信息门户

网页url为http://idas.uestc.edu.cn/authserver/loginservice=http%3A%2F%2Fportal.uestc.edu.cn%2F
可以看到登陆原本是不存在验证码的,但是当输入密码错误达五次以上后就会出现验证码,下面将给出不带验证码和带验证码的代码。
F12打开浏览器自带检查工具(我用的是Chorme)
手动登陆页面,在检查页面Network页面发现有一个login开头的包疑似我们需要的包,点击进入获取post的header/data,目标url等用于我们的登陆。包打开后观察header信息如下:

这里我们看到了一些包的信息,继续往后看会有更多有用的信息,包括post的data和header等。


在这里我们很高兴的看到了我们向服务器发送的请求包数据信息,里面包括我们的登陆名和密码和一些其他认证信息等,值得注意的是,It这个数据包是随cookie登陆信息变化的,这要求我们要加载cookie信息,或者使BeautifulSoup解析器获取。

不带验证码的模拟登陆

全部代码如下:

# -*- coding: utf-8 -*-
import urllib
import urllib2
from bs4 import BeautifulSoup
import http.cookiejar#Get It
def clt(url):response = urllib2.urlopen(url)data = response.read()soup = BeautifulSoup(data, 'html.parser', from_encoding='utf-8')link = soup.find_all('input')aa = link[2]aa=str(aa)return aa[38:-3]##获取cookies,发送认证信息
def post(username,password,url):cj = http.cookiejar.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))urllib2.install_opener(opener)header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3)\AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36r)'}##生成学号,登陆密码信息postdata = urllib.urlencode({'username': username,'password': password,'lt': clt('http://idas.uestc.edu.cn/authserver/login'),'dllt': 'userNamePasswordLogin','execution': 'e1s1','_eventId': 'submit','rmShown': '1'})postdata = postdata.encode('utf-8')req = urllib2.Request(url, postdata, headers=header)result = opener.open(req)return resultif __name__ =='__main__':url = 'http://idas.uestc.edu.cn/authserver/login?service=http%3A%2F%2Fportal.uestc.edu.cn%2F'username='你的学号'password = '你的密码'print '尝试密码为'+passwordresponse =post(username,password,url)data = response.read()print data

一般这样就能够顺利登陆了,如果成功,print data打印出网页信息如下:

如果不成功,返回信息中将会包括输入验证码等部分如下:

带验证码的模拟登陆和自动识别是否成功

基于返回的html信息我们可以找到两者的不同,发现其中有个字符串在登陆成功时不存在,在登陆成功时存在,标记一个flag来表明是否登陆成功。
如果带有验证码,则需要知道验证码的地址,将其下载到本地后进行下一步操作。
验证码url为:http://idas.uestc.edu.cn/authserver/captcha.html?ts=437
解决方法1:手动识别
解决方法2:运用PIL图像识别库读取验证码并将其变为字符串即可。

手动输入验证码

下面给出解决方法1的代码:
Soluion 1

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 18 18:46:53 2018@author: 10091
"""
import urllib
import urllib2
from bs4 import BeautifulSoup
import http.cookiejar
from PIL import Image#获取It信息
def clt(url):response = urllib2.urlopen(url)data = response.read()soup = BeautifulSoup(data, 'html.parser', from_encoding='utf-8')##print(data.decode())link = soup.find_all('input')aa = link[2]aa=str(aa)return aa[38:-3]#爬取验证码
def get_captcha():url = 'http://idas.uestc.edu.cn/authserver/captcha.html?ts=437'name ='Captcha.jpg'html = urllib2.urlopen(url)cont = html.read()with open(name,'wb') as f:f.write(cont)#发送数据包尝试连接
def post(username,password,url):#获取cookies保持登陆cj = http.cookiejar.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))urllib2.install_opener(opener)user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36r)'header = {'User-Agent': user_agent}##生成学号,登陆密码信息get_captcha()image = Image.open('Captcha.jpg')image.show()captchResponse = raw_input('What you see in chapcha:')postdata = urllib.urlencode({'username': username,'password': password,'captchaResponse': captchResponse,'lt': clt('http://idas.uestc.edu.cn/authserver/login'),'dllt': 'userNamePasswordLogin','execution': 'e1s1','_eventId': 'submit','rmShown': '1'})postdata = postdata.encode('utf-8')req = urllib2.Request(url, postdata, headers=header)result = opener.open(req)return result#解析返回的数据查看是否登陆成功
def jiexi(data):soup = BeautifulSoup(data, 'html.parser',from_encoding = 'utf-8')links=soup.find_all(href="getBackPasswordMainPage.do")  return len(links)#运行程序
if __name__ == '__main__':url = 'http://idas.uestc.edu.cn/authserver/login\service=http%3A%2F%2Fportal.uestc.edu.cn%2F'flag = 1exp_num =0while flag!=0 :username='你的学号'password = '你的密码'exp_num = exp_num+1print '尝试密码为'+passwordresponse =post(username,password,url)data = response.read()flag =jiexi(data)if exp_num > 5:print '输入次数过多,退出'breakif flag == 0 :print '登陆成功'

运用PIL自动识别

Solution 2
运用PIL库进行自动识别,由于可能存在识别错误的情况,进行多次登陆,直到成功为止退出。

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 18 19:11:00 2018@author: 10091import urllib
import urllib2
from bs4 import BeautifulSoup
import http.cookiejarimport pytesseract
from PIL import Image#获取It信息
def clt(url):response = urllib2.urlopen(url)data = response.read()soup = BeautifulSoup(data, 'html.parser', from_encoding='utf-8')##print(data.decode())link = soup.find_all('input')aa = link[2]aa=str(aa)return aa[38:-3]#爬取验证码
def get_captcha():url = 'http://idas.uestc.edu.cn/authserver/captcha.html?ts=437'name ='Captcha.jpg'html = urllib2.urlopen(url)cont = html.read()with open(name,'wb') as f:f.write(cont)
#解析验证码
def read_captcha():image = Image.open('Captcha.jpg')vcode = pytesseract.image_to_string(image)word = ''for i in vcode:if (ord(i)>=48 and ord(i)<=57) or (ord(i)>=65 and ord(i)<=90) \or (ord(i)>=97 and ord(i)<=122):word += icaptcha = str(word).replace(' ','')print '识别验证码为:' + captchareturn captcha#发送数据包尝试连接
def post(username,password,url):#获取cookies保持登陆cj = http.cookiejar.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))urllib2.install_opener(opener)user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36r)'header = {'User-Agent': user_agent}##生成学号,登陆密码信息get_captcha()captchResponse = read_captcha()postdata = urllib.urlencode({'username': username,'password': password,'captchaResponse': captchResponse,'lt': clt('http://idas.uestc.edu.cn/authserver/login'),'dllt': 'userNamePasswordLogin','execution': 'e1s1','_eventId': 'submit','rmShown': '1'})postdata = postdata.encode('utf-8')req = urllib2.Request(url, postdata, headers=header)result = opener.open(req)return result
#解析返回的数据查看是否登陆成功
def jiexi(data):soup = BeautifulSoup(data, 'html.parser',from_encoding = 'utf-8')links=soup.find_all(href="getBackPasswordMainPage.do")  return len(links)#运行程序
if __name__ == '__main__':url = 'http://idas.uestc.edu.cn/authserver/login?service=http%3A%2F%2Fportal.uestc.edu.cn%2F'username='2016100104028'password = '106711'flag=1exp_num =0while flag!=0 :exp_num = exp_num+1print '尝试密码为'+passwordresponse =post(username,password,url)data = response.read()flag =jiexi(data)if exp_num>5:print '次数过多,你可能密码错了'breakif flag == 0 :print '登陆成功'

如果操作太过频繁。建议使用代理和延时等。

python2实现模拟登陆UESTC信息门户相关推荐

  1. java模拟京东登陆_requests+beautifulsoup模拟登陆京东

    最近需要实现获取个人京东订单信息的功能,利用了requests+beautifulsoup来实现. requests是python的第三方库,相比之前常用的python标准库中的urllib2,req ...

  2. 利用selenium+chrome模拟登陆合工大信息门户并进行自动填写测评

    最近学校要填写对于老师的评教,不填写的就无法进行下周的选课∑^)/ 我这么懒,自然不想一个一个点进去填写,想到最近在学爬虫,干脆写一个爬虫帮我弄算了 ╭~~~╮ (o~.~o) 首先打开我们学校的信息 ...

  3. 电子科技大学信息门户模拟登录

    首先抓包或者按下F12进入浏览器开发者模式进行分析: 由这张图我们可以得出我们填写完学号和密码后post的表单包含的内容有username,password,lt,execution,eventld, ...

  4. python github登陆_用Python模拟登陆GitHub并获取信息

    最近在研究如何对搜狗搜索公众号文章进行爬取,由于需要用到Cookies,所以这回先了解下Cookies的相关知识. 搜狗的反爬有点厉害,即使我用了高匿代理,它还是会提醒我IP访问过于频繁,然后跳转验证 ...

  5. winform模拟登陆网页_Python爬虫使用selenium爬取群成员信息(全自动实现自动登陆)...

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: python小爬虫 PS:如有需要Python学习资料的小伙伴可以 ...

  6. 模拟登陆新版正方教务管理系统【可以获取学生基本/课表信息】

    写在前面 博主登陆现在还可以正常使用,但是后面登陆成功,获取信息啥的有问题 登陆还是按照学长的来,模拟登陆新版正方教务管理系统 开始 学校教务系统改版,我直接copy博主代码获取学籍那里一直是获取到的 ...

  7. 拱拱Lite开发(3):三翼页及湘大文库下载实现(解析网页获取信息及模拟登陆)

    因为没有三翼新闻及湘大文库的API,简单的方法行不通就只能绕远啦,我们这次来解析网页,嗯,是个体力活其实.因为网页HTML也是有一定格式的,所以只要网页结构不进行大的改动,我们就可以一直这样解析网页获 ...

  8. python获取app信息的库_基于Python的模拟登陆获取脉脉好友信息

    代码已经上传到github上 简介: 这是一个基于python3而写的爬虫,爬取的网站的脉脉网(https://maimai.cn/),在搜索框中搜索"CHO",并切换到" ...

  9. Python模拟登陆大连交通大学教务在线

    Python模拟登陆大连交通大学教务在线,使用wxPython做的界面,urllib2发送数据,可以实现登陆后获取首页登陆数据的功能,目前只做登陆,其他的暂时没做 Python2.7代码 # -*- ...

最新文章

  1. 数据还原到指定时间点的处理示例
  2. 反向telnet连接
  3. Python 技术篇-利用pyqt5库监听剪切板变动,clipboard.dataChanged.connect()剪切板监听
  4. python如何让用户输入文件名并打开文件_(Python)如何让用户打开文本文件然后更改整数/数字...
  5. java wav 时间,Java-调整WAV文件的播放速度
  6. Tomcat根据JSP生成Servlet机制解析
  7. spring boot的hello world小实验
  8. oracle游标多线程,多线程jdbc游标分页查询原理 oracle和mysql分页
  9. SCOM 2012 R2监控Microsoft Azure服务(2)配置Azure监控
  10. db2如何锁定一张表_办公必备的保护工作表技巧,你会了么?
  11. C#动态生成Word文档并填充数据(二)
  12. NodeJs数据库CRUD操作
  13. 文献阅读:《Generative Adversarial Active Learning for Unsupervised Outlier Detection》-2020 trans
  14. 亿级爆款背后,网易云音乐的生长之道
  15. LOLBox多玩饭盒Android源码
  16. 一个视频分割为多个视频片段
  17. unity shader 溶解,上下左右,cutoff
  18. androidstudio上传自己的lib到Jcenter
  19. 连载:中国最早的一代官派留学生--留美幼童 (结尾)
  20. python之父:一个合格的python程序员,应该从这三本书入手!

热门文章

  1. laravel 163邮箱
  2. 那些跟马化腾一起创业的亿万富翁们
  3. C#程序运行不了CLR20R3解决方法
  4. java进程运行一段时间停止_kafka总是在启动一段时间后自动停止
  5. 达观数据智能问答技术研究
  6. Linux查看文件内容的几种方法
  7. “C语言的核心是指针,灵魂是算法“那么指针为什么被誉为C语言灵魂呢?
  8. 最全的java对接微信小程序客服功能实现(包含自动回复文本消息、图片消息,进入人工客服)
  9. 双模sa_一文看懂华为Mate 20 X 5G版中的“双模、SA和NSA”
  10. python如何播放视频_python如何播放视频