万年日历
发布日期:2021-05-14 05:50:53 浏览次数:17 分类:精选文章

本文共 4113 字,大约阅读时间需要 13 分钟。

#Coding使用gbk编码
import requests
from bs4 import BeautifulSoup
import xlwt
#获取一年数据,以字典返回
def getYear():
yearDic = {}
week = 2 # 2019年1月1日为星期二
year = 2019 # 可改为所需年份,注意初始星期需调整
urlList = []
base_url = 'https://wannianrili.51240.com/ajax/?q=-{}-{}&v=18121803'
#构造每个月的接口链接
for month in range(1,13):
if month <10:
url = base_url.format(str(year), '0'+str(month))
else:
url = base_url.format(str(year), str(month))
urlList.append(url)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
for i in range(len(urlList)):
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 += '(休)'
week = int(week)
if week == 7:
week = 0
week +=1
dayList.append(str(week))
dayList.append(gStr)
dayList.append(nStr)
monthDic = {}
monthDic[gStr] = dayList
yearDic[i+1] = monthDic
return yearDic, year
#获取每个月的星期标题坐标
def coordinates():
yearCoorDic = {}
x = 2 # 初始横坐标
for month in range(1,13):
monthList = []
num = x
if month == 1 or month ==5 or month ==9:
num = x
if month <5:
for day in range(7):
tList = []
cross = x + 14
column = day + num
tList.append(cross)
tList.append(column)
if day ==6:
num = column +2
monthList.append(tList)
elif 5 < month <9:
for day in range(7):
tList = []
cross = x +14*2
column = day + num
tList.append(cross)
tList.append(column)
if day ==6:
num = column +2
monthList.append(tList)
elif month >8:
for day in range(7):
tList = []
cross = x +14*2
column = day + num
tList.append(cross)
tList.append(column)
if day ==6:
num = column +2
monthList.append(tList)
yearCoorDic[month] = monthList
return yearCoorDic
#制作Excel模板
def template():
book = xlwt.Workbook()
sheet = book.add_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()
alignment = xlwt.Alignment()
alignment.vert = 0x01
styleRed.alignment = alignment
styleRed.font = xlwt.Font(name=u'微软雅黑', color_index=2, bold=True)
styleRed1.alignment = alignment
styleBlack.alignment = alignment
styleBlack_.alignment = alignment
styleBlack1_.alignment = alignment
titleStyle.alignment = alignment
styleContent.alignment = alignment
#添加字体样式
font = xlwt.Font(name=u'微软雅黑')
fontRed = xlwt.Font(name=u'微软雅黑', color_index=2, bold=True)
fontBlack = xlwt.Font(name=u'微软雅黑', color_index=0, bold=True)
fontBlack_ = xlwt.Font(name=u'微软雅黑', color_index=23, bold=True)
#设置单元格样式
for cell in [styleRed, styleRed1, styleBlack, styleBlack_, styleBlack1_, titleStyle, styleContent,
alignment]:
上一篇:MySQL的cast函数
下一篇:python 利用爬虫获取页面上下拉框里的所有国家

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月13日 10时24分02秒