0x00:python 语言有众多的第三方库,并且本身拥有的库也非常多,这些库也成就了python 这门语言无所不能的称号。
0x01:urllib 是python 处理网页的一个模块,该模块是python 的自带模块,使用时可以直接导入,
beauitfulsoup 是python的一个第三方模块,该模块提供了对网页html 和 xml 文件解析的功能。
0x02:这个脚本的目的,实现某软的打卡签到功能,配合linux cron 即可实现全自动打卡。网站会跟踪cookie 所以脚本中需要有对cookie的处理功能,网站使用js生成参数的方法
防止使用脚本通过post固定的账号密码进行打卡,故脚本需要有模拟登陆的过程而不是直接post 账号密码到一个固定的网站中。
打卡网站使用jsp编写,脚本的流程为:进入打卡网站首页,从网页内读出需要提交到下一个网页的一些参数,
例如在正常的网站中,用户明和密码是为‘user’ ='abc123','password'='ccc456',但是在这个网站中表单中用户名和密码的name 为js在生成网页的时候生成的。
这就需要读取网页并解析出该name。第二步为使用第一个网页解析出的参数加上账号密码一起post到第二个网站中,即完成网站的认证过程。
第三步,根据第二个网站的页面内容找出需要post的表单以及url,生成所需头文件以及post内容提交到第三个网页中去,完成打卡网页的打卡按钮动作。
0x03:脚本
__author__ = 'niem' import urllib.request import urllib.parse import re from bs4 import BeautifulSoup import http.cookiejar class neusoft: def __init__(self): self.__cookie_support = urllib.request.HTTPCookieProcessor(http.cookiejar.LWPCookieJar()) self.__opener = urllib.request.build_opener(self.__cookie_support,urllib.request.HTTPHandler) def makeheader(self): UserAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0' Connection = 'keep-alive' AcceptLanguage = 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' AcceptEncoding = 'gzip, deflate' Accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' headers = [('User-Agent',UserAgent), ('Connection',Connection), ('Accept-Language',AcceptLanguage), ('Accept-Encoding',AcceptEncoding), ('Accept',Accept), ] return headers def prepare_request(self,postdata,url): urllib.request.install_opener(self.__opener) url_code_post_data = urllib.parse.urlencode(postdata) postdata_utf8 = url_code_post_data.encode('utf-8') if not postdata: request = urllib.request.Request(url) else : request = urllib.request.Request(url,data = postdata_utf8) for (k,v) in self.makeheader(): request.add_header(k,v) return request neu = neusoft() request1 = neu.prepare_request('','http://url1') response1 = urllib.request.urlopen(request1) page1 = response1.read() soup1 = BeautifulSoup(page1.decode('utf-8'),'html.parser') neusoft_key_tag = soup1.find(attrs = {'name':'neusoft_key'}) neusoft_key = neusoft_key_tag.attrs['value'] user_name_name_tag = soup1.find(attrs={'name':re.compile('ID.*')}) user_name_name = user_name_name_tag.attrs['name'] user_pass_name_tag = soup1.find(attrs = {'name':re.compile('KEY.{20,}')}) user_pass_name = user_pass_name_tag.attrs['name'] post_data2 = [(user_pass_name,'bbbbbb'), (user_name_name,'aaaaa'), ('neusoft_key',neusoft_key), ('login','true'), ('neusoft_attendance_online',''), ] request2 = neu.prepare_request(post_data2,'http://url2') response2 = urllib.request.urlopen(request2) page2 = response2.read() soup2 = BeautifulSoup(page2.decode('GBK'),'html.parser') currentempoid_tag = soup2.find(attrs = {'value':re.compile('[0-9]*')}) currentempoid = currentempoid_tag.attrs['value'] post_data3 = [('currentempoid',currentempoid)] request3 = neu.prepare_request(post_data3,'http://url3') response3 = urllib.request.urlopen(request3)