记基于python3的新手爬虫的一次应用————给自己

写这个脚本的起因是为了在补实习日志偷懒上才有的,用到的技术也就一个get提交和post提交,但因为本人还是个新手,所以写出来时还是很开心的。【可惜学校没多久就学聪明了,这个也就废了】

from http.cookiejar import CookieJar  
from urllib.request import build_opener, HTTPCookieProcessor, Request  
from urllib.parse import urlencode  
from PIL import Image  
import pytesseract
import urllib
import http.cookiejar
from urllib import request
import time
import re
def numbers_to_rtype(argument,types):
    global days
    if types == 1 :
        if argument == 1:
            return nullRest(days)
        elif argument == 2:
            return singleRest(days)
        elif argument == 3:
            return doubleRest(days)
        else :
            print('没别的功能啦,别要求太多!')
    elif types == 2 :
        if argument == 1:
            return YnullRest(days)
        elif argument == 2:
            return YsingleRest(days)
        elif argument == 3:
            return YdoubleRest(days)
        else :
            print('没别的功能啦,别要求太多!')
    else :
        print('没别的功能啦,别要求太多!')


def nullRest(days):
    global sdays
    global stitle
    for i in range(days):
        nday = sdays
        dayW = time.localtime(nday)[6]
        sdays = sdays + 1*60*60*24
        times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
        titles.append("实习第%s天"%stitle)
        stitle = stitle + 1

def singleRest(days):
    global sdays
    global stitle
    for i in range(days):
        nday = sdays
        dayW = time.localtime(nday)[6]
        sdays = sdays + 1*60*60*24
        if dayW == 6:
                continue
        times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
        titles.append("实习第%s天"%stitle)
        stitle = stitle + 1

def doubleRest(days):
    global sdays
    global stitle
    for i in range(days):
        nday = sdays
        dayW = time.localtime(nday)[6]
        sdays = sdays + 1*60*60*24
        if dayW == 6 or dayW == 5:
            continue
        times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
        titles.append("实习第%s天"%stitle)
        stitle = stitle + 1

def YnullRest(days):
    global sdays
    global stitle
    for i in range(days):
        nday = sdays
        dayW = time.localtime(nday)[6]
        sdays = sdays + 1*60*60*24
        stitle = stitle + 1
        times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
        titles.append("实习第%s天"%stitle)

def YsingleRest(days):
    global sdays
    global stitle
    for i in range(1,days):
        nday = sdays
        dayW = time.localtime(nday)[6]
        sdays = sdays + 1*60*60*24
        if dayW == 6:
                titles.append("休息")
                times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
                continue
        times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
        titles.append("实习第%s天"%stitle)
        stitle = stitle + 1

def YdoubleRest(days):
    global sdays
    global stitle
    for i in range(1,days):
        nday = sdays
        dayW = time.localtime(nday)[6]
        sdays = sdays + 1*60*60*24
        if dayW == 6 or dayW == 5:
            titles.append("周末双休")
            times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
            continue
        times.append(time.strftime("%Y-%m-%d",time.localtime(nday)))
        titles.append("实习第%s天"%stitle)
        stitle = stitle + 1

def getVerify():
    ##获取验证码
    getVerify = "验证码url"
    getVerify = opener.open(Request(getVerify)).read().decode('utf-8')
    imgurl=re.search('验证码图片路径的正则表达式',getVerify).group(1)
    imgurl="网站当前url"+imgurl
    #打开图片并写入  
    res=urllib.request.urlretrieve(imgurl,'.\\verify\\v.jpg') 
    #保持图像 
    image = Image.open('.\\verify\\v.jpg')
    vv = pytesseract.image_to_string(image)#因为学校的验证码特别耿直,成功率特别高,连图像学习都不用学到。
    return getCookie(vv) 

def getCookie(vv):
    global uid
    global pwd
    global logined_in
    data = urlencode({  
    #post请求要提交的数据
    }).encode('utf-8')
    header = {
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding":"gzip,deflate,sdch",
    "Accept-Language":"zh-cn,zh;q=0.8,en;q=0.6",
    "Connection":"keep-alive",
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
    "Upgrade-Insecure-Requests":"1",
    }
    reqPost = Request(login,data,header)
    logined_in = opener.open(reqPost).read().decode('utf-8')
    cannotLogin=re.search('<p class="att3">.+?</p>',logined_in)
    ##用来确定自己是否登录
    if cannotLogin :
        return getVerify()
    else:   
        myName=re.search('<span id="Left1_lblName" style="width:100%;">(.+?)</span>',logined_in).group(1)
        print(myName)##打印自己名字出来,毕竟登录成功
        return autoLog()

def autoLog():
    global times
    global titles
    for i in range(len(times)):
        add_link = "日志url"
        add_header = {
        ##随便复制一下
        }
        add_data = urlencode({  
        ##需要get提交的信息
        }).encode('utf-8')
        reqAdd = Request(add_link,add_data,add_header)
        opener.open(reqAdd).read().decode('utf-8')

uid = input("账号:")
pwd = input("密码:")
sday = input("实习开始日期:")
eday = str(time.strftime("%Y-%m-%d", time.localtime()))
rtype = int(input("休假类型(1为无休,2为单休,3为双休):"))
wtype = int(input("日志类型(1为周末没写,2为周末有写):"))
print('Waiting...')
times = []#时间周期
titles = []#标题集
days = int((int((time.mktime(time.strptime(eday,"%Y-%m-%d")))-int(time.mktime(time.strptime(sday,"%Y-%m-%d")))))/(24*60*60))+1
sdays = int(time.mktime(time.strptime(sday,"%Y-%m-%d")))
stitle = 1
##需补交时间
numbers_to_rtype(rtype,wtype)
login = "登录url"
###cookie点
cj = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.open(login)
###
getVerify()#一步到位
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值