吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6530|回复: 35
收起左侧

[Python 原创] [python]获取一年日历数据并写入excel表格中

  [复制链接]
朝夕忆浅 发表于 2019-1-11 11:35
本帖最后由 wushaominkk 于 2019-1-11 16:36 编辑

简单来说就是先爬取数据,然后把数据写入到excel中,做成日历表~~哈哈哈,经供参考和学习哈~~
下面是代码,写的不够规范,别见笑哈哈哈
[Asm] 纯文本查看 复制代码
# coding=gbk
import requests
from bs4 import BeautifulSoup
import xlwt
# 获取一年数据,以字典返回
def getYear():
    yearDic = {}
    week = 2 # 初始星期,2019年1月1日为星期二
    year = 2019 # 年份 想要哪年的就改成那年的 记得修改上面的初始星期
    urlList = []
    uUrl = 'https://wannianrili.51240.com/ajax/?q={}-{}&v=18121803'
    # 构造每个月的接口链接
    for y in range(1,13):
        if y<10:
            rUrl = uUrl.format(year,'0'+str(y))
            urlList.append(rUrl)
        else:
            rUrl = uUrl.format(year,y)
            urlList.append(rUrl)
    headers = {
        'User-Agent': 'Mozilla / 5.0(Windows NT 10.0; Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 68.0.3440.106Safari / 537.36',
    }
    for i in range(0,len(urlList)):
        monthDic = {}
        html = requests.get(urlList[i], headers=headers)
        soup = BeautifulSoup(html.content, 'lxml')
        riqiList = soup.find_all(class_='wnrl_riqi')
        for riqi in riqiList:
            dayList = []
            g = riqi.find_all(class_='wnrl_td_gl')
            n = riqi.find_all(class_='wnrl_td_bzl')
            gStr = g[0].get_text()
            nStr = n[0].get_text()
            # 找到该元素视为法定节假日
            if riqi.find_all(class_='wnrl_riqi_xiu'):
                nStr+='(休)'
            dayList.append(week)
            # 到星期日后重置为0
            if week == 7:
                week = 0
            week+=1
            dayList.append(gStr)
            dayList.append(nStr)
            monthDic[gStr] = dayList
        yearDic[i+1] = monthDic
    return yearDic,year
# 初始每个月的星期标题单元格坐标,页面3*4网格方式展示
def coordinates():
    yearCoorDic = {}
    x = 2 # 初始横坐标
    y = 0 # 初始纵坐标
    interval = 2 # 月份之间的纵坐标间距
    for i in range(1,13):
        monthList = []
        # 月份为1,5,9 时num重新初始为0
        if i == 1 or i == 5 or i == 9:
            num = y
        if i < 5:
            # 循环7次,为星期一到星期天
            for k in range(7):
                tList = []
                # cross =  x # 横
                # 每次纵坐标+1
                column =  k + num # 纵
                tList.append(x)
                tList.append(column)
                # 记住最后一次(星期天)纵坐标+月份间隔,为下个月(星期一)的纵坐标值
                if k == 6:
                    num = column+interval
                monthList.append(tList)
        if i>4 and i<9:
            for k in range(7):
                tList = []
                # 横坐标方向的单元格数,计算得出日+农历最大占用12行。这里给14行算上横坐标间距
                cross =  x + 14 # 横
                column =  k + num # 纵
                tList.append(cross)
                tList.append(column)
                if k == 6:
                    num = column+interval
                monthList.append(tList)
        if i>8 and i<13:
            for k in range(7):
                tList = []
                cross =  x + 14*2 # 横
                column =  k + num # 纵
                tList.append(cross)
                tList.append(column)
                if k == 6:
                    num = column+interval
                monthList.append(tList)
        yearCoorDic[i] = monthList
    return yearCoorDic
def template():
    book = xlwt.Workbook()  # 新建一个excel对象
    sheet = book.add_sheet('2019年日历表')  # 添加一个sheet页
    month_style = xlwt.easyxf('font: height 280;')  # 定义月份标题单元格高度
    week_style = xlwt.easyxf('font: height 340;')  # 定义星期单元格高度
    Content_style = xlwt.easyxf('font: height 280;') # 定义日期和农历单元格高度
    styleRed = xlwt.XFStyle()  # 创建一个样式对象,初始化样式  适用于周末单元格
    styleRed1 = xlwt.XFStyle()  # 创建一个样式对象,初始化样式  适用于周末单元格且节日名过长时
    styleBlack = xlwt.XFStyle()  # 创建一个样式对象,初始化样式 适用于工作日单元格
    styleBlack_ = xlwt.XFStyle()  # 创建一个样式对象,初始化样式 适用于农历单元格
    styleBlack1_ = xlwt.XFStyle() # 创建一个样式对象,初始化样式 适用于农历单元格且节日名过长时
    titleStyle = xlwt.XFStyle()  # 创建一个样式对象,初始化样式  适用于月份标题单元格
    styleContent = xlwt.XFStyle() # 创建一个样式对象,初始化样式  适用于日期和农历单元格
    # 设置单元格样式  通用
    al = xlwt.Alignment()
    al.horz = 0x02  # 设置水平居中
    al.vert = 0x01  # 设置垂直居中
    # 设置单元格样式  适用于styleRed1 styleBlack1_
    al1 = xlwt.Alignment()
    al1.vert = 0x01  # 设置垂直居中
    # 设置单元格样式  适用于周末和法定节假日
    fnt = xlwt.Font()
    fnt.bold = True  # 字体加粗
    fnt.name = u'微软雅黑'  # 设置其字体为微软雅黑
    fnt.colour_index = 2  # 字体红色
    # 设置单元格样式  适用于工作日和星期
    fnt1 = xlwt.Font()
    fnt1.bold = True  # 字体加粗
    fnt1.name = u'微软雅黑'  # 设置其字体为微软雅黑
    fnt1.colour_index = 0  # 字体黑色
    # 设置单元格样式  适用于农历
    fnt1_ = xlwt.Font()
    fnt1_.bold = True  # 字体加粗
    fnt1_.name = u'微软雅黑'  # 设置其字体为微软雅黑
    fnt1_.colour_index = 23  # 字体灰色
    # 设置单元格样式  适用于月份标题显示
    fnt2 = xlwt.Font()
    fnt2.bold = True  # 字体加粗
    fnt2.name = u'微软雅黑'  # 设置其字体为微软雅黑
    fnt2.colour_index = 1  # 字体黑色
    pattern = xlwt.Pattern()  # Create the pattern
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    pattern.pattern_fore_colour = 8  # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on..
    # 应用单元格样式
    styleRed.alignment = al
    styleRed.font = fnt
    styleRed1.alignment = al1
    styleRed1.font = fnt
    # 应用单元格样式
    styleBlack.alignment = al
    styleBlack.font = fnt1
    # 应用单元格样式
    styleBlack_.alignment = al
    styleBlack_.font = fnt1_
    styleBlack1_.alignment = al1
    styleBlack1_.font = fnt1_
    # 应用单元格样式
    titleStyle.alignment = al
    titleStyle.font = fnt2
    titleStyle.pattern = pattern
    styleContent.alignment = al
    # 获取每个月星期标题坐标初始值
    yearCoorDic = coordinates()
    print('正在获取日历数据...')
    # 获取一年的数据
    yearDic,year = getYear()
    titList = list(yearCoorDic.keys())
    print('%s年数据获取成功,正在写入模板...'%year)
    for i in range(len(titList)):  # 12个月份
        # 获取第一个月份的星期初始坐标
        titcoorList = yearCoorDic[titList[i]]
        # 设置应用月份标题的高度
        first_row = sheet.row(titcoorList[0][0]-1)
        first_row.set_style(month_style)
        sheet.write_merge(titcoorList[0][0]-1, titcoorList[0][0]-1, titcoorList[0][1], titcoorList[-1][1], '{}月'.format(i+1), titleStyle)  # 合并单元格并写入月份
        # 根据坐标写入星期标题
        for j in range(0,len(titcoorList)):
            # 设置应用星期标题宽度
            first_col = sheet.col(titcoorList[j][1])
            first_col.width = 95 * 25
            # 设置应用星期标题的高度
            first_row = sheet.row(titcoorList[j][0])
            first_row.set_style(week_style)
            if j+1 == 1:
                title = '星期一'
            elif j+1 == 2:
                title = '星期二'
            elif j+1 == 3:
                title = '星期三'
            elif j+1 == 4:
                title = '星期四'
            elif j+1 == 5:
                title = '星期五'
            elif j+1 == 6:
                title = '星期六'
            else:
                title = '星期日'
            sheet.write(titcoorList[j][0], titcoorList[j][1], title , styleBlack if j+1<6 else styleRed)
        # 初始每个星期横坐标初始值
        oneNum,twoNum,threeNum,fourNum,fiveNum,sixNum,sevenNum = 1,1,1,1,1,1,1 # 总觉得有简化的方法 这样写感觉好傻-.-
        # 获取第一个月份数据的键值列表
        daykeyList = list(yearDic[titList[i]].keys())
        for k in range(len(daykeyList)): # 每个月的日期
            dayList = yearDic[titList[i]][daykeyList[k]] # 获取每日的列表值['2','01','元旦']  第一个值为星期几,第二个为日期,第三个为农历或节假日
            # 判断每个月第一天为星期几,若为星期二,则把星期一的横坐标初始值初始为3,即星期一为空,以此类推
            if k == 0:
                if dayList[0]==2:
                    oneNum = 3
                if dayList[0]==3:
                    oneNum,twoNum = 3,3
                if dayList[0]==4:
                    oneNum,twoNum,threeNum = 3,3,3
                if dayList[0]==5:
                    oneNum,twoNum,threeNum,fourNum= 3,3,3,3
                if dayList[0]==6:
                    oneNum,twoNum,threeNum,fourNum,fiveNum= 3,3,3,3,3
                if dayList[0]==7:
                    oneNum,twoNum,threeNum,fourNum,fiveNum,sixNum= 3,3,3,3,3,3
            # 判断日期是星期几,执行对应语句
            if 1 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[0][0] + oneNum)
                first_row.set_style(Content_style)
                # 写入日期,日期横坐标的值为初始星期标题坐标值+oneNum初始值
                sheet.write(titcoorList[0][0] + oneNum, titcoorList[0][1], dayList[1], styleRed if '休' in dayList[2] else  styleBlack)
                # 初始值+1 为下个农历节假日的初始值
                oneNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[0][0] + oneNum)
                first_row.set_style(Content_style)
                # 写入农历或节假日 初始星期标题+oneNum初始值
                sheet.write(titcoorList[0][0] + oneNum, titcoorList[0][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                oneNum+=1
            if 2 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[1][0] + twoNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[1][0] + twoNum, titcoorList[1][1], dayList[1], styleRed if '休' in dayList[2] else  styleBlack)
                twoNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[1][0] + twoNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[1][0] + twoNum, titcoorList[1][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                twoNum+=1
            if 3 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[2][0] + threeNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[2][0] + threeNum, titcoorList[2][1], dayList[1], styleRed if '休' in dayList[2] else  styleBlack)
                threeNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[2][0] + threeNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[2][0] + threeNum, titcoorList[2][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                threeNum+=1
            if 4 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[3][0] + fourNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[3][0] + fourNum, titcoorList[3][1], dayList[1], styleRed if '休' in dayList[2] else  styleBlack)
                fourNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[3][0] + fourNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[3][0] + fourNum, titcoorList[3][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                fourNum+=1
            if 5 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[4][0] + fiveNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[4][0] + fiveNum, titcoorList[4][1], dayList[1], styleRed if '休' in dayList[2] else  styleBlack)
                fiveNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[4][0] + fiveNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[4][0] + fiveNum, titcoorList[4][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                fiveNum+=1
            if 6 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[5][0] + sixNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[5][0] + sixNum, titcoorList[5][1], dayList[1], styleRed)
                sixNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[5][0] + sixNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[5][0] + sixNum, titcoorList[5][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                sixNum+=1
            if 7 == dayList[0]:
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[6][0] + sevenNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[6][0] + sevenNum, titcoorList[6][1], dayList[1], styleRed)
                sevenNum+=1
                # 设置应用单元格的高度
                first_row = sheet.row(titcoorList[6][0] + sevenNum)
                first_row.set_style(Content_style)
                sheet.write(titcoorList[6][0] + sevenNum, titcoorList[6][1], dayList[2], styleRed if '休' in dayList[2] else  styleBlack1_ if len(dayList[2])>4 else styleBlack_)
                sevenNum+=1
    book.save('%s年日历表.xls'%year)
    print('保存成功,程序执行完毕...')
template()

表格截图~
menu.saveimg.savepath20190111113314.jpg

评论看到有人要成品的,我把成品上传上来了,有需要的下载解压就可以了

2019年日历表.zip (4.93 KB, 下载次数: 178)

免费评分

参与人数 9吾爱币 +13 热心值 +9 收起 理由
zhaolisheng + 1 + 1 热心回复!
做客人间 + 1 + 1 不会爬取数据,嘿嘿
adq_cq + 1 + 1 谢谢@Thanks!
wgy1213 + 1 + 1 先不管有用没用,lz辛苦,我反手就赞
speedboy + 2 + 1 谢谢@Thanks!
tianyou + 1 + 1 我很赞同!
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
蚂蚱丶 + 1 + 1 谢谢@Thanks!
Mainos + 2 + 1 原创带源码必须给分

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hillman323 发表于 2019-1-12 18:53
楼主,代码运行不了,帮看看
File "/Users/a.py", line 7
    &#160;&#160;&#160;&#160;yearDic = {}
    ^
SyntaxError: invalid syntax
 楼主| 朝夕忆浅 发表于 2019-1-11 11:59
瑞浩娱乐 发表于 2019-1-11 11:57
大佬,能教下怎么弄吗?有代码不知道怎么弄出来,刚好公司还没发日历,一下没日历感觉很不习惯,需要这个

你要的话我可以把生成好的模板给你
xjh88232259 发表于 2019-1-11 11:55
瑞浩娱乐 发表于 2019-1-11 11:57
大佬,能教下怎么弄吗?有代码不知道怎么弄出来,刚好公司还没发日历,一下没日历感觉很不习惯,需要这个
lyliucn 发表于 2019-1-11 12:08
Python功能好强呀。
xiuyuan1314 发表于 2019-1-11 12:19
朝夕忆浅 发表于 2019-1-11 11:59
你要的话我可以把生成好的模板给你

你好,我需要一份,能不能也发给我一份成品啊,谢谢!!
 楼主| 朝夕忆浅 发表于 2019-1-11 12:25
xiuyuan1314 发表于 2019-1-11 12:19
你好,我需要一份,能不能也发给我一份成品啊,谢谢!!

成品上传到帖子里了,下载就好了
罗茂松 发表于 2019-1-11 12:36
这个有点意思
破解丨家 发表于 2019-1-11 12:36
这个就厉害了
fihuang2315 发表于 2019-1-11 12:37
辛苦了楼主。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 10:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表