Python模拟登陆4:进入图书馆预约系统查看是否预约成功

代码仅供测试方便所用。

这个图书管理系统是asp.net EXTJS写的 在分析页面的时候非常复杂,一度差点放弃。不过还是慢慢的进步。还顺便学了一下正则表达式。这个代码的作用就是登陆进系统,然后获取预约信息。代码有点乱,以后在整理,这里先存档

这个网址校外ip不能访问,所以代码仅供参考,部分逻辑还是能够套用的

#-*- coding: utf-8 -*-
#!usr/bin/env python
import os
import urllib2
import urllib
import cookielib
import re
from bs4 import BeautifulSoup

Login_URL='http://10.21.16.215'
Main_URL='http://10.21.16.215/Florms/FormSYS.aspx'
Seats_URL='http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSubmitWindow.aspx?parameters=89B0F70D378F61A7BA0411B5439DBC76521E906832306CE419BCCD5F85380BE374557D246EE19799E9A8992E242BEC3261F078093CE5602B53E9BE3DD0AD5696E85B6BEECD6835BA'
username='XXXX'
password='XXXX'
roomNum='202006'
def login():
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    pre_response=opener.open(Login_URL)
    soup=BeautifulSoup(pre_response.read())
    div=soup.find(id='__EVENTVALIDATION')
    attr1=dict(div.attrs)['value']
    #print attr1
    div=soup.find(id='__VIEWSTATE')
    attr2=dict(div.attrs)['value']
    #print attr2
    #login
    LoginData = {
            'txtUserName':username,
            'txtPassword':password,
            '__EVENTVALIDATION':attr1,
            '__VIEWSTATE':attr2,
            'cmdOK.x':'40',
            'cmdOK.y':'14',
            };
    login_req = urllib2.Request(Login_URL, urllib.urlencode(LoginData));
    login_req.add_header('User-Agent', "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
    login_response=opener.open(login_req)
    print 'login success'
    soup=BeautifulSoup(opener.open(Main_URL).read())
    div=soup.find(id='__VIEWSTATE')
    viewstate=dict(div.attrs)['value']
    print 'start get seats info'
    room_url='http://10.21.16.215/FunctionPages/SeatBespeak/SeatLayoutHandle.ashx'
    #datetime=raw_input('input your date of tomorrw(2015-01-13):  ')
    datetime='2015-01-13'
    datetime=datetime+' 0:00:00'
    print datetime
    postdata={
        'roomNum':'202006',
        'date':datetime,
        'divTransparentTop':'0',
        'divTransparentLeft':'0'
    }
    headers={
            'X-Requested-With': 'XMLHttpRequest',
            'Accept-Language': 'zh-cn',
            'Accept-Encoding': 'gzip, deflate',
            'ContentType': 'application/x-www-form-urlencoded;',
            'Host': '10.21.16.215',
            'Origin':'http://10.21.16.215',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
            'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeatLayout.aspx?roomId=202006&date=635566176000000000',
            'Connection' : 'Keep-Alive',
    }
    #req = urllib2.Request(room_url, urllib.urlencode(postdata),headers);
    #response=opener.open(req)
    #result=response.read()
    #print result
    #get Seats
    SeatData = {
            '__EVENTTARGET':'ContentPanel1$btnBespeak',
            '__EVENTARGUMENT':' ',
            '__VIEWSTATE':'/wEPDwULLTEzMzAxMjU4NDFkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYEBQVGb3JtMgUNQ29udGVudFBhbmVsMQUYQ29udGVudFBhbmVsMSRidG5CZXNwZWFrBRZDb250ZW50UGFuZWwxJGJ0bkNsb3NlFnml1ZniYWarY7w+39y6XMauzvQuUhZHmd7FIJHtCzY=',
            '__EVENTVALIDATION':'/wEWAgKz1fTFAgL+mI+WBmMYZmWmY/5C6T+zShCagVwIeSBhVaQY5pYrGTq0aLBk',
            'roomOpenTime':'8:30',
            'X_CHANGED':'false',
            'X_TARGET':'ContentPanel1_btnBespeak',
            'Form2_Collapsed':'false',
            'ContentPanel1_Collapsed':'false',
            'X_AJAX':'true',
            #'X_STATE':'eyJGb3JtMl9jdGwwMF9sYmxSb29tTmFtZSI6eyJUZXh0Ijoi5LiJ5qW85Y2V5Lq656CU5L+u5a6kIn0sIkZvcm0yX2N0bDAxX2xibFNlYXRObyI6eyJUZXh0IjoiMzEzIn0sIkZvcm0yX2N0bDAyX2xibGJlZ2luRGF0ZSI6eyJUZXh0IjoiMjAxNS0xLTEyIn0sIkZvcm0yX2N0bDAzX2xibEVuZERhdGUiOnsiVGV4dCI6Ijc6MDDoh7MxMDowMCJ9fQ==',
            };
    headers = {
            'X-Requested-With': 'XMLHttpRequest',
            'Accept-Language': 'zh-cn',
            'Accept-Encoding': 'gzip, deflate',
            'ContentType': 'application/x-www-form-urlencoded; chartset=UTF-8',
            'Host': '10.21.16.215',
            'Origin':'http://10.21.16.215',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
            'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSubmitWindow.aspx?parameters=89B0F70D378F61A7BA0411B5439DBC76521E906832306CE419BCCD5F85380BE374557D246EE19799E9A8992E242BEC3261F078093CE5602B53E9BE3DD0AD5696E85B6BEECD6835BA',
            'Connection' : 'Keep-Alive'
    }
    '''
    loc=1
    while loc>=0:
        result=get_chair(SeatData,headers,opener)
        loc=result.find('失败')
        print 'failed and try'
    '''
    print 'Get your chair ,Success!!!'

    #send qing shui he
    '''
    postdata={
        '__EVENTTARGET':'Form2$ctl00$btnnewdate',
        '__EVENTARGUMENT':' ',
        '__VIEWSTATE':'/wEPDwUKLTM3MzI4Mzg0MmQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgYFBUZvcm0yBRZGb3JtMiRjdGwwMCRkZGxMaWJyYXJ5BRdGb3JtMiRjdGwwMCRkcFN0YXJ0RGF0ZQUWRm9ybTIkY3RsMDAkYnRubmV3ZGF0ZQUMZ3JpZFJvb21MaXN0BQpXaW5kb3dFZGl05pJoDo97fsj4YPsAfwutGBWhcfnVi5TdDL2VUl7PIH8=',
        'Form2$ctl00$ddlLibrary':'02',
        'Form2$ctl00$dpStartDate':'2015-01-12',
        'X_CHANGED':'true',
        'WindowEdit_Hidden':'true',
        'X_TARGET':'Form2_ctl00_btnnewdate',
        'Form2_Collapsed':'false',
        'gridRoomList_Collapsed':'false',
        'gridRoomList_SelectedRowIndexArray':' ',
        'WindowEdit_Collapsed':'false',
        'X_AJAX':'true',
        'X_STATE':'eyJGb3JtMl9jdGwwMF9kZGxMaWJyYXJ5Ijp7IkRhdGFUZXh0RmllbGQiOiJOYW1lIiwiRGF0YVZhbHVlRmllbGQiOiJObyIsIlhfSXRlbXMiOltbIjAxIiwi5rKZ5rKz5qCh5Yy65Zu+5Lmm6aaGIiwxXSxbIjAyIiwi5riF5rC05rKz5qCh5Yy65Zu+5Lmm6aaGIiwxXSxbIjAzIiwi5Zu+5Lmm6aaG5Yib5paw5a6e6aqM5a6kIiwxXV0sIlNlbGVjdGVkVmFsdWUiOiIwMiJ9LCJGb3JtMl9jdGwwMF9kcFN0YXJ0RGF0ZSI6eyJNaW5EYXRlIjoiMjAxNS0wMS0xM1QwMTowNDo1Ni40OTY1KzA4OjAwIiwiVGV4dCI6IjIwMTUtMDEtMTMifSwiZ3JpZFJvb21MaXN0Ijp7IlJlY29yZENvdW50IjozLCJYX1Jvd3MiOnsiVmFsdWVzIjpbWyIyMDIwMDUiLCLkuozmpbzljZXkurrnoJTkv67lrqQiLCLmuIXmsLTmsrPmoKHljLrlm77kuabppoYiLCIyMiIsIjIyIiwiPGEgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIGNsYXNzPVwieC1pdGVtLWRpc2FibGVkXCIgZGlzYWJsZWQ9XCJkaXNhYmxlZFwiPjxpbWcgc3JjPVwiL0ltYWdlcy9pY29uL2J1bGxldF9jcm9zcy5wbmdcIiAgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIC8+PC9hPiJdLFsiMjAyMDA2Iiwi5LiJ5qW85Y2V5Lq656CU5L+u5a6kIiwi5riF5rC05rKz5qCh5Yy65Zu+5Lmm6aaGIiwiMjQiLCIyNCIsIjxhIGV4dDpxdGlwPVwi6aKE57qm5pe26Ze05Li677yaMTM6MDDliLAyMzo1OVwiICBjbGFzcz1cIngtaXRlbS1kaXNhYmxlZFwiIGRpc2FibGVkPVwiZGlzYWJsZWRcIj48aW1nIHNyYz1cIi9JbWFnZXMvaWNvbi9idWxsZXRfY3Jvc3MucG5nXCIgIGV4dDpxdGlwPVwi6aKE57qm5pe26Ze05Li677yaMTM6MDDliLAyMzo1OVwiICAvPjwvYT4iXSxbIjIwMjAwNyIsIuWbm+alvOWNleS6uueglOS/ruWupCIsIua4heawtOays+agoeWMuuWbvuS5pummhiIsIjQiLCI0IiwiPGEgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIGNsYXNzPVwieC1pdGVtLWRpc2FibGVkXCIgZGlzYWJsZWQ9XCJkaXNhYmxlZFwiPjxpbWcgc3JjPVwiL0ltYWdlcy9pY29uL2J1bGxldF9jcm9zcy5wbmdcIiAgZXh0OnF0aXA9XCLpooTnuqbml7bpl7TkuLrvvJoxMzowMOWIsDIzOjU5XCIgIC8+PC9hPiJdXSwiRGF0YUtleXMiOltbbnVsbF0sW251bGxdLFtudWxsXV0sIlN0YXRlcyI6W1tdLFtdLFtdXX19fQ==',
    }
    headers={
            'X-Requested-With': 'XMLHttpRequest',
            'Accept-Language': 'zh-cn',
            'Accept-Encoding': 'gzip, deflate',
            'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': '10.21.16.215',
            'Origin':'http://10.21.16.215',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
            'Referer' : 'http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx',
            'Connection' : 'Keep-Alive',
    }
    req = urllib2.Request('http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx', urllib.urlencode(postdata),headers);
    response=opener.open(req)
    result=response.read()
    print result
    # first page
    headers={
            #'X-Requested-With': 'XMLHttpRequest',
            'Accept-Language': 'zh-cn',
            'Accept-Encoding': 'gzip, deflate',
            #'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': '10.21.16.215',
            #'Origin':'http://10.21.16.215',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
            'Referer' : 'http://10.21.16.215/Florms/FormSYS.aspx',
            'Connection' : 'Keep-Alive',
    }
    req = urllib2.Request('http://10.21.16.215/FunctionPages/SeatBespeak/BespeakSeat.aspx',headers=headers);
    response=opener.open(req)
    result=response.read()
    #print result
    #print result
    '''
    print get_log()
def get_chair(SeatData,headers,opener):
    seat_req = urllib2.Request(Seats_URL, urllib.urlencode(SeatData),headers);
    seat_response=opener.open(seat_req)
    result=seat_response.read()
    return result
#get chair log
def get_log():
    url='http://10.21.16.215/FunctionPages/ReaderLog/SelectBespeakLog.aspx';
    headers={
            #'X-Requested-With': 'XMLHttpRequest',
            'Accept-Language': 'zh-cn',
            'Accept-Encoding': 'gzip, deflate',
            #'ContentType': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': '10.21.16.215',
            #'Origin':'http://10.21.16.215',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
            'Referer' : 'http://10.21.16.215/Florms/FormSYS.aspx',
            'Connection' : 'Keep-Alive',
    }
    req = urllib2.Request(url,headers=headers);
    response=urllib2.urlopen(req)
    result=response.read()
    #line = '三楼单人研修室", "304", "已确认", "15-01-10 13:00:18", "15-01-11 08:30:00", "", "在'
    #print result.find('三楼单人研修室')
    #matchObj=re.search(  r'三楼单人研修室(.*?)08:30:00', result, re.M|re.I)
    #print matchObj.group(1)
    p=re.compile(r'"三楼单人研修室"(.*?)08:30:00')
    #print p.findall(result)[-1]
    flag=p.findall(result)[-1].find('已取消')
    if flag>=0:
        return True
    else:
        return False

if __name__=='__main__':
    login()
时间: 2024-10-18 14:41:22

Python模拟登陆4:进入图书馆预约系统查看是否预约成功的相关文章

Python模拟登陆万能法-微博|知乎

Python模拟登陆让不少人伤透脑筋,今天奉上一种万能登陆方法.你无须精通HTML,甚至也无须精通Python,但却能让你成功的进行模拟登陆.本文讲的是登陆所有网站的一种方法,并不局限于微博与知乎,仅用其作为例子来讲解. 用到的库有"selenium"和"requests".通过selenium进行模拟登陆,然后将Cookies传入requests,最终用requests进行网站的抓取.优点就是不但规避了"selenium"其本身抓取速度慢的问题

Python模拟登陆万能法

此文转自:https://zhuanlan.zhihu.com/p/28587931   转录只是为了方便学习,感谢他的分享 Python模拟登陆让不少人伤透脑筋,今天奉上一种万能登陆方法.你无须精通HTML,甚至也无须精通Python,但却能让你成功的进行模拟登陆.本文讲的是登陆所有网站的一种方法,并不局限于微博与知乎,仅用其作为例子来讲解. 用到的库有"selenium"和"requests".通过selenium进行模拟登陆,然后将Cookies传入reque

python模拟登陆实践

对于一些需要登陆的网址爬取数据,需要登录才能访问.本文介绍如何利用python进行模拟登陆 准备工具: 1.搭建django 框架 用于模拟登陆 2.fiddler抓包工具,chrome浏览器 3.pycharm   编辑器 步骤: 1.开启django 服务,这里就不多描述,直接百度一下,就能找到很多答案(记得创建一个superuser,以便后面的登陆 ) 进入 http://127.0.0.1:8000/admin/     这是django自带的后台,进行登陆时,django自带了  cs

通过抓包,实现Python模拟登陆各网站,原理分析!

一.教程简介 1.1 基本介绍 通过分析登陆流程并使用 Python 实现模拟登陆到一个实验提供的网站,在实验过程中将学习并实践 Python 的网络编程,Python 实现模拟登陆的方法,使用 Firefox 抓包分析插件分析网络数据包等知识. 模拟登录可以帮助用户自动化完成很多操作,在不同场合下有不同的用处,无论是自动化一些日常的繁琐操作还是用于爬虫都是一项很实用的技能.本课程通过 Firefox 和 Python 来实现,环境要求如下: Python 库:urllib, http.cook

Python模拟登陆练习——imooc.com登陆

写下这篇文章的时候,是博主学习python的第三天( 也许是第四天:( ),python是博主接触的第二门解释型语言(第一门是javascript). 讲真在很久之前就想要用博客记录自己的学习历程了,然而就像写日记一样,写着写着就放弃了-.- so今天决定给自己一个好的开端~ 博主的学习方式是直奔目的,遇到问题百度各种博客,网站,百度找不到google找,就这样.这种学习方式是真的见效快,但显而易见,基础会比较薄弱. 因此学习python的基本语法,就直奔爬虫了! ---------------

Python模拟登陆:模拟登陆电子科大信息门户测试

最近在学习Python的网络操作,网上有很多模拟登陆方法.这里做个笔记. 没有验证码的还是比较容易的. #coding:utf-8 import re import urllib import urllib2 import cookielib import os import json import sys import time from urllib import quote,unquote from urllib2 import HTTPError from urllib2 import

Python模拟登陆

模拟人人登陆 #encoding=utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,password): #登陆页面,能够通过抓包工具分析获得,如fiddler.wireshark login_page = "http://www.renren.com/PLogin.do" try: #获得一个cookieJar实例 cj = cookielib.CookieJar() #cook

Python模拟登陆3: 进入选课系统(手工输入验证码阶段)

其实最想做的是选课插件,于是这次试试了下选课系统,但是选课系统 有验证码. 很是蛋疼. 需要识别. 但是现在可以用另一种方法.就是下载到本地手动输入.也只需要一次就够了.因为登陆成功后就可以随意操作其他东西了. 后面再学习验证码的智能识别. 首先主要是模拟逻辑是.先设置一个cookie存储器,用它去访问验证码链接.然后会得到cookie.还有下载好的验证码图片,就在本地.然后再把验证码,用户名,密码,和那个cookie一起提交到登陆.于是cookie就一致了. 有点不舒服的就是需要在本地看下那个

Python模拟登陆2:获取电子科大研究生成绩

前面小试了一把 能够成功登陆,但是这下要获取成绩可是废了好多功夫.首先他这个登陆只是登陆了一个frame框架页面,里面还有个js的二次跳转页面.还会自动登陆一次.只有在IE或者谷歌等http抓包才看的出来隐藏的地址. 开始以为很简单 直接在第二个登陆页面提交post 谁知道 他会自动跳转到第一个登陆页面 前端白痴的我有点乱入了. 最后试来试去, 偷偷去看了下cookies ,尝试性的直接提交了cookies想不到 成功了. 但是这个cookies是在成绩页面 不知道是request还是respo