Python统计天气数据

在这里插入图片描述

import urllib.request,re,datetime
def get_html(city,year,month):
    url="https://m.tianqi.com/lishi/%s/%s%s.html"%(city,year,month)
    request=urllib.request.Request(url)
    request.add_header("User-Agent","Mozilla/5.0")
    return urllib.request.urlopen(request).read().decode("UTF-8")
# print(get_html("haidian","2019","08"))
dates,xq,tq,qw,highs,lows=[],[],[],[],[],[]
city="haidian"
year="2019"
months=["%02d"%i for i in range(1,13)]
prev_day=datetime.datetime(2018,12,31)

for month in months:
    html=get_html(city,year,month)
    nospace_text="".join(html.split())
    pattern=re.compile('<divclass="weatherbox">(.*?)</div><divclass="clearline1">')
    div_list=re.findall(pattern,nospace_text)
    pattern1 = re.compile('<dlclass="table_day15">(.*?)</dl>')
    dls = re.findall(pattern1, div_list[0])
    for dl in dls:
            date_pattern=re.compile('<ddclass="date">(.*?)</dd>')
            date_dd=re.findall(date_pattern,dl)
            d_str=year+"/"+date_dd[0][0:5]
            # print(d_str)
            date_pattern = re.compile('<i>(.*?)</i>')
            date_xq = re.findall(date_pattern, dl)
            date_pattern=re.compile('<ddclass="txt1">(.*?)</dd>')
            date_tq=re.findall(date_pattern,dl)
            date_pattern = re.compile('<ddclass="txt2">(.*?)</dd>')
            date_qw = str(re.findall(date_pattern, dl)).replace("<b>","").replace("</b>","").replace("℃","")
            try:
                cur_day=datetime.datetime.strptime(d_str,'%Y/%m/%d')
            except ValueError:
                print(cur_day,"数据错误")
            else:
                diff=cur_day-prev_day
                if diff!=datetime.timedelta(days=1):
                    print('在%s之前丢失数据'%d_str)
                dates.append(d_str)
                xq.append(date_xq)
                tq.append(date_tq)
                qw.append(date_qw)
                qws=date_qw[2:-2].split("~")
                lows.append(int(qws[0]))
                highs.append(int(qws[1]))
                prev_day=cur_day

import pygal
bar =pygal.Line()
bar.title="北京市海淀区%s年气温分析"%year
bar.add("最低气温",lows)
bar.add("最高气温",highs)
bar.x_labels =dates
bar.x_labels_major=dates[::10]
bar.show_minor_x_labels=False
bar.x_title="日期"
bar.y_title="气温(摄氏度)"
bar.x_label_rotation=45
# bar.legend_at_bottom=True
# bar.margin=35
bar.show_y_guides=True
bar.show_x_guides=False
bar.render_to_file("temp.svg")


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魑魅魍魉9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值