python爬虫及语言处理练习

家里长辈想要一款定制的月份工作表制作软件,试着稍微编一编,目前完成度如下(至八月份)。

进度节点清单:

python爬虫及语言处理练习_第1张图片

目前进度:

目前时间显示:

     

时间表生成:

python爬虫及语言处理练习_第2张图片

周末时间分析:

政府政策爬取及处理:

python爬虫及语言处理练习_第3张图片

政府政策节日分析:

python爬虫及语言处理练习_第4张图片

政府政策调休分析:

python爬虫及语言处理练习_第5张图片

人员信息录入:

python爬虫及语言处理练习_第6张图片

特殊节假日录入:

人员信息及特殊节假日查询:

目前代码总览:

def now():  #当前时间
    import time
    todayyear = time.localtime()[0] #目前年份
    todaymonth = time.localtime()[1] #目前月份
    todayday = time.localtime()[2] #目前日
    return "目前时间:\n"+str(todayyear)+"年"+str(todaymonth)+"月"+str(todayday)+"日"


def allyearcalendar():  #全年日历显示
    import time
    import calendar
    todayyear = time.localtime()[0]  # 目前年份
    return calendar.calendar(todayyear)


def mcalendar(year, month):  #全月日历显示
    import time
    import calendar
    return calendar.month(year, month)


def weekend(todayyear, todaymonth):  #周末分析
    weekend1 = []
    import calendar
    a = calendar.monthcalendar(todayyear, todaymonth)
    for i in range(0,len(a)):
        for j in range(5,7):
            if a[i][j] !=0:
                weekend1.append(a[i][j])
    for i1 in range(0, len(weekend1)):
        day = str(todaymonth)+"月"+str(weekend1[i1])+"日"
        if i1 == 0:
            print("在"+day,sep=",",end =",")
        if i1 != len(weekend1)-1 and i1 != 0:
            print(day, sep=",", end=",")
        if i1 == len(weekend1)-1:
            print(day+"为周末", sep=",", end="。\n")
    return weekend1


def holiday(per, last, expectyear):  #国务院节假日通知爬取
    import requests
    import re
    from fake_useragent import UserAgent    #反爬虫头部信息模拟
    ua = UserAgent()
    headers = {'User-Agent': ua.ie}
    web = 'https://baike.baidu.com/item/国务院办公厅关于' + expectyear + '年部分节假日安排的通知'
    result = requests.get(url=web,headers=headers)
    reg2 = r'
(.*)
' reg_ques = re.compile(reg2) a1 = reg_ques.findall(result.text) iper= 0 ilast = 0 for i in range(0, len(a1)): if per in a1[i]: iper = i if last in a1[i]: ilast = i del a1[ilast:len(a1)] del a1[0:iper+1] return a1 def holidaydesign1(holiday, ChineseHolidaylist, todayyear ):#国务院节假日信息处理(假日) special = [] class specialtime(object): # 特殊时间点结构化(节日) def __init__(self, Name, Year, Month, Day, Num): self.Name = name self.Year = year self.Month = month self.Day = day self.Num = num def check(self): print("假期名称为: %s,假期开始年为: %s," % (self.Name,self.Year),end='') print(" , 假期开始月为: %s ,假期开始日为: %s ,假期放多少天:%s天。" % (self.Month, self.Day, self.Num)) import jieba name = " " year = " " month = " " day = " " num = " " Truenum = [] Truenum2 = [] Trueinfo = [] ChineseNum = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'] for i1 in range(0, len(holiday)): for i2 in range(0, len(ChineseNum)): if ChineseNum[i2] in holiday[i1]: Truenum.append(i1) [Truenum2.append(i) for i in Truenum if not i in Truenum2] #查重 for i in range(0,len(Truenum2)):#节日处理 Trueinfo.append(holiday[Truenum2[i]]) content = Trueinfo[i] seg_list = jieba.lcut(content, cut_all=False) #自然语言处理 judge = 0 judge2 = 0 todayyearsave = todayyear for i1 in range(0, len(seg_list)): for i2 in range(0, len(ChineseHolidaylist)): if ChineseHolidaylist[i2] in seg_list[i1] and judge == 0: name = seg_list[i1] save1 = seg_list[i1] judge = 1 if ':' in seg_list[i1] and judge == 1: save2 = i1 judge = 3 if '年' in seg_list[i1] and judge2 == 0: todayyearsave = seg_list[i1-1] judge2 = 1 if '月' in seg_list[i1] and judge == 3: save22 = seg_list[i1-1] judge = 4 if '日' in seg_list[i1] and judge == 4: save23 = seg_list[i1-1] judge = 5 if '放假' in seg_list[i1] and judge == 5: save3 = i1 judge = 6 if '共' in seg_list[i1] and judge == 6: save4 = seg_list[i1+1] special.append(save1) name = save1 year = todayyearsave month = save22 day = save23 num = save4 special[len(special) - 1] = specialtime(name, year, month, day, num) break if '周末' in seg_list[i1] and judge == 6: special.append(save1) name = save1 year = todayyearsave month = save22 day = save23 num = "1(不加周末)" special[len(special) - 1] = specialtime(name, year, month, day, num) break return special def holidaydesign2(holiday, ChineseHolidaylist, todayyear): # 国务院节假日信息处理(调休) special = [] class worktime(object): # 特殊时间点结构化(节日) def __init__(self, Year, Month, Day): self.Year = year self.Month = month self.Day = day def check(self): print("调休开始年为: %s" % (self.Year), end='') print(" , 调休开始月为: %s ,调休开始日为: %s" % (self.Month, self.Day)) import jieba year = " " month = " " day = " " Truenum = [] Truenum2 = [] Trueinfo = [] ChineseNum = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'] for i1 in range(0, len(holiday)): for i2 in range(0, len(ChineseNum)): if ChineseNum[i2] in holiday[i1]: Truenum.append(i1) [Truenum2.append(i) for i in Truenum if not i in Truenum2] # 查重 for i in range(0, len(Truenum2)): # 节日处理 Trueinfo.append(holiday[Truenum2[i]]) content = Trueinfo[i] seg_list = jieba.lcut(content, cut_all=False) # 自然语言处理 judge = 0 judge2 = 0 todayyearsave = todayyear for i1 in range(0, len(seg_list)): if '年' in seg_list[i1] and judge2 == 0: todayyearsave = seg_list[i1 - 1] judge2 = 1 if '年' in seg_list[i1] and judge2 == 1: todayyearsave = seg_list[i1-1] if '。' in seg_list[i1] and judge == 0: judge = 1 if '月' in seg_list[i1] and judge == 1: save1 = seg_list[i1-1] judge = 2 if '日' in seg_list[i1] and judge == 2: save2 = seg_list[i1-1] judge = 3 if judge == 3: special.append(str(i)) year = todayyearsave month = save1 day = save2 special[len(special)-1] = worktime(year, month, day) judge = 1 judge2 = 0 if '上班' in seg_list[i1]: break return special def extratimeadd1(names,years): #自行添加时间点(节日) special = [] class specialtime(object): # 特殊时间点结构化(节日) def __init__(self, Name, Year, Month, Day): self.Name = name self.Year = year self.Month = month self.Day = day def check(self): print("额外假期名称为: %s,额外假期年为: %s," % (self.Name, self.Year), end='') print("额外假期月为: %s ,额外假期日为: %s " % (self.Month, self.Day)) name = names year = str(years) month = '' day = '' judge = 1 while judge == 1: month = input('额外假期月为:') day = input('额外假期日为:') special.append(name+'1') special[len(special)-1] = specialtime(name, year, month, day) judge = int(input("输入1继续操作:")) return special def numberinfo(): #人员信息录入 class number(object): def __init__(self, Name, Info1, Info2, Info3, Info4, Info5): self.Name = name self.Info1 = info1 self.Info2 = info2 self.Info3 = info3 self.Info4 = info4 self.Info5 = info5 def check(self): print("人员信息为: %s,信息1为: %s" % (self.Name, self.Info1), end='') print(" , 信息2为: %s ,信息3为: %s ,信息4为:%s ,信息5为:%s" % (self.Info2, self.Info3, self.Info4, self.Info5)) name = "" info1 = "" info2 = "" info3 = "" info4 = "" info5 = "" special = [] print("开始人员信息录入") judge = 1 while judge == 1: name = input("输入人员姓名:") info1 = input("输入人员信息1:") info2 = input("输入人员信息2:") info3 = input("输入人员信息3:") info4 = input("输入人员信息4:") info5 = input("输入人员信息5:") special.append(len(special)) special[len(special)-1] = number(name, info1, info2 , info3 , info4 , info5) special[len(special)-1].check() judge = int(input("输入1继续操作:")) print('录入完成') return special todayyear = int(input("输入工作表年份:")) #期望年份 todaymonth = int(input("输入工作表月份:")) #期望月份 extraname = '调休' print(mcalendar(todayyear,todaymonth)) ChineseHolidaylist = ['元旦', '春节', '清明节', '劳动节', '端午节', '中秋节', '国庆节', '妇女节', '青年节', '儿童节', '中国人民解放军建军纪念日'] Per = '假日安排公布' Last = '节假日期间,各地区、各部门要妥善安排好值班和安全、保卫等工作,遇有重大突发事件,要按规定及时报告并妥善处置,确保人民群众祥和平安度过节日假期' print(now()) Holiday = holiday(Per, Last, str(todayyear)) Weekend = weekend(todayyear, todaymonth) Holidayinfo = holidaydesign1(Holiday, ChineseHolidaylist, todayyear) Workinfo = holidaydesign2(Holiday, ChineseHolidaylist, todayyear) print(Weekend) Numberinfo = numberinfo() Extratimeadd1 = extratimeadd1(extraname, todayyear) for i in range(0, len(Holiday)): print(Holiday[i]) for i in range(0, len(Holidayinfo)): Holidayinfo[i].check() for i in range(0, len(Workinfo)): Workinfo[i].check() for i in range(0, len(Numberinfo)): Numberinfo[i].check() for i in range(0, len(Extratimeadd1)): Extratimeadd1[i].check()

小结:

大致做到节点4了,长辈也不着急用,再加上大连疫情复发,做的便比较慢。

其实已经半个月没有再写了(´-ω-`)

编程思路就两步:

能够获取制作工作表需要用到的数据的代码,以及通过用户操作能处理相关数据生成符合要求的工作表的代码。

你可能感兴趣的:(python)