首页 >后端开发 >Python教程 >Python Requests模拟登录实现图书馆座位自动预约

Python Requests模拟登录实现图书馆座位自动预约

不言
不言原创
2018-04-27 10:40:524677浏览

这篇文章主要为大家详细介绍了Python Requests的模拟登录,Python实现图书馆座位自动预约,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Python实现图书馆座位自动预约的具体代码,供大家参考,具体内容如下

配置

通过公网主机定时运行脚本,并发送邮件到自己的qq邮箱,这样在微信就会有消息提示是否预约成功

vim /etc/crontab

设置每到早上7:01自动运行脚本即可

程序流程

(以yuyue.juneberry.cn网站为例)

  • get访问登录页面,获取cookie和表单里面的隐藏post字段

  • 构造登录post数据,加入从表单里面拿到的隐藏post字段

  • post构造后的数据,模拟登录,激活cookie(使cookie有登入权限)

  • get访问座位预约界面,激活cookie(使cookie有预约座位权限)

  • post预约请求,实现预约座位

  • 解析返回结果,判断是否成功,并邮件提醒

要点

  • requests库中的requests.session() 能够创建可传递cookies的会话

  • 拿到c726a8da2e1b0a50980b1ad4157a77d3的数据并传递到post的数据中

  • 抓包判断网站逻辑,筛选出各个请求的参数,并在程序中实现

函数解释

  • class FUCK()主类

  • _get_date_str(self):获取当前日期,并加上一天,用这个函数构造url的特征字段(图书馆设置提前一天预约座位)

  • def _get_order_url(self):构造"预约座位"的post目标url

  • def _get_static_post_attr:这个函数解析get请求的返回页面,并从中提取出c726a8da2e1b0a50980b1ad4157a77d3的字段,用于之后的构造post数据

  • def login(self):实现登录功能

  • def run(self):实现座位预约功能

  • def _is_success(self, text):判断预约结果

  • def error_log_once(self, text='default error (once)'):

  • def error_log(self, text='default error'):这两个函数设置程序状态为"已经出错"或者"未出错"状态(用于自动化运行的时候避免将重复的错误信息写入日志)

  • def error_log(self, text='default error'):单次将错误信息写入本地日志

  • sendmail.send_mail()邮件发送模块

代码及注释

# /bin/python
# -*- coding:utf-8 -*-
import time
import sys
import requests
from bs4 import BeautifulSoup
from mail import sendmail

__author__ = 'xy'

# 主类
class FUCK():
 def __init__(self, username, password, seatNO, mailto):
 """
  以四个参数初始化,用户名,密码,要预约的座位号,接受预约结果提醒邮件的邮箱
 """
  self.username = username
  self.password = password
  self.seatNO = seatNO
  self.mailto = mailto
  self.base_url = 'http://yuyue.juneberry.cn'
  self.login_url = 'http://yuyue.juneberry.cn'
  self.order_url = self._get_order_url()

  self.login_content = ''
  self.middle_content = ''
  self.final_content = ''

  self.s = requests.session() # 创建可传递cookies的会话

  # post data for login
  self.data1 = {
   'subCmd': 'Login',
   'txt_LoginID': self.username, # S+学号
   'txt_Password': self.password, # 密码
   'selSchool': 60, # 60表示北京交通大学
  }

  # post data for order a seat
  self.data2 = {
   'subCmd': 'query',
  }

  # 自定义http头,然而我在程序里并没有使用
  self.headers = {
   'Connection': 'keep-alive',
   'Content-Type': 'application/x-www-form-urlencoded',
  }

  self.login()
  self.run()
  self._is_success(self.final_content)

  # 怀疑程序出错时,取消下行注释,可打印一些参数
  # self._debug()

 def _get_date_str(self):
  s = time.localtime(time.time())
  ########333
  date_str = str(s.tm_year) + '%2f' + str(s.tm_mon) + '%2f' + str(s.tm_mday + 1)
  date_str = date_str.replace('%2f1%2f32', '%2f2%2f1') \
   .replace('%2f2%2f29', '%2f3%2f1') \
   .replace('%2f3%2f32', '%2f4%2f1') \
   .replace('%2f4%2f31', '%2f5%2f1') \
   .replace('%2f5%2f32', '%2f6%2f1') \
   .replace('%2f6%2f31', '%2f7%2f1') \
   .replace('%2f7%2f32', '%2f8%2f1') \
   .replace('%2f8%2f32', '%2f9%2f1') \
   .replace('%2f9%2f31', '%2f10%2f1') \
   .replace('%2f10%2f32', '%2f11%2f1') \
   .replace('%2f11%2f31', '%2f12%2f1') \
   .replace('%2f12%2f32', '%2f1%2f1')
  return date_str

 def _get_order_url(self):
  return "http://yuyue.juneberry.cn/BookSeat/BookSeatMessage.aspx?seatNo=101001" + self.seatNO + "&seatShortNo=01" + self.seatNO + "&roomNo=101001&date=" + self._get_date_str()

 def _get_static_post_attr(self, page_content, data_dict):
  """
  拿到<input type=&#39;hidden&#39;>的post参数,并添加到post_data中
  """
  soup = BeautifulSoup(page_content, "html.parser")
  for each in soup.find_all(&#39;input&#39;):
   if &#39;value&#39; in each.attrs and &#39;name&#39; in each.attrs:
    data_dict[each[&#39;name&#39;]] = each[&#39;value&#39;] # 添加到login的post_data中
    # self.data2[each[&#39;name&#39;]] = each[&#39;value&#39;] # 添加到order的post_data中
  return data_dict

 def _debug(self):

  print self.order_url
  print self.data1
  print self.data2
  print self.headers
  print self.s.cookies

  # print self.login_content
  # print self.middle_content
  print self.final_content

 def login(self):
  homepage_content = self.s.get(self.base_url).content
  self.data1 = self._get_static_post_attr(homepage_content, self.data1)
  r = self.s.post(self.login_url, self.data1)
  self.login_content = r.content

 def run(self):

  # 这个get的意思是:原先的cookie没有预约权限,
  # 访问这个get之后,会使cookie拥有预约权限,从而执行下一个post
  self.middle_content = self.s.get(&#39;http://yuyue.juneberry.cn/BookSeat/BookSeatListForm.aspx&#39;).content

  # 经测试,这个post只需要一个subCmd的参数就可以正常返回,因此不必根据get内容更新post参数
  # self.data2 = self._get_static_post_attr(middle_content, self.data2)

  # 这个post请求完成了预约功能!
  r = self.s.post(self.order_url, self.data2)

  self.final_content = r.content

 def _is_success(self, text):
  """
  接受最终的html内容,判断是否成功,并触发日志记录和邮件提醒
  """
  if &#39;<h5 id="MessageTip">已经存在有效的预约记录。</h5>&#39; in text:
   self.clear_error_once(&#39;[done!] You already ordered a seat!&#39;)
  elif &#39;<h5 id="MessageTip">选择的日期不允许预约。</h5>&#39; in text:
   self.clear_error_once(&#39;[done!] Date is wrong!&#39;)
  elif &#39;<h5 id="MessageTip">所选座位已经被预约。</h5>&#39; in text:
   self.clear_error_once(&#39;[done!] This seat is not available, maybe taken by others!&#39;)
  elif &#39;<h5 id="MessageTip">座位预约成功&#39; in text:
   self.clear_error_once(&#39;[done!] Success! An email is sending to you!&#39;)
   sendmail.send_mail(&#39;BJTU Library Seat_NO:&#39; + self.seatNO + &#39;ordered!&#39;,
        &#39;Sending by robot. Do not reply this mail!&#39;, self.mailto)
  else:
   self.error_log_once(&#39;Error! 302 to login page&#39;)

 def error_log_once(self, text=&#39;default error (once)&#39;):
  try:
   is_error_file = open(&#39;./isopen_xy.txt&#39;, &#39;r&#39;)
  except:
   is_error_file = open(&#39;./isopen_xy.txt&#39;, &#39;w&#39;)
  if &#39;1&#39; not in is_error_file.read():
   print &#39;writting error to log...&#39;
   self.error_log(text)
  else:
   print &#39;already written to log&#39;
  is_error_file.close()
  sendmail.send_mail(&#39;BJTU_Library system error once !&#39;, &#39;error text!&#39;)

 def error_log(self, text=&#39;default error&#39;):
  is_error_file = open(&#39;./isopen_xy.txt&#39;, &#39;w&#39;)
  is_error_file.write(&#39;1\n&#39;)
  is_error_file.close()

  f = open("./log_xy.txt", &#39;a&#39;)
  f.write(time.strftime("%Y-%m-%d %X", time.localtime()) + text + &#39;\n&#39;)
  f.close()

 def clear_error_once(self, text=&#39;success&#39;):
  print text
  is_error_file = open(&#39;./isopen_xy.txt&#39;, &#39;w&#39;)
  is_error_file.write(&#39;0\n&#39;)
  is_error_file.close()


if __name__ == &#39;__main__&#39;:
 if len(sys.argv) < 5:
  print &#39;Usage: python library.py [username] [password] [seat_NO] [email]&#39;
  print &#39;eg. python library.py S13280001 123456 003 XXXX@qq.com\n&#39;
  print &#39;Any problems, mail to: i[at]cdxy.me&#39;
  print &#39;#-*- Edit by cdxy 16.03.24 -*-&#39;
  sys.exit(0)
 else:
  FUCK(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

相关推荐:

python读写json文件案列详解(附代码)

PYTHON正则表达式之re模块使用说明

以上是Python Requests模拟登录实现图书馆座位自动预约的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn