python+pygal分析杭州某年的天气

1、分析下雨的天气占比

#coding:utf-8

import urllib.request,re
import pygal,datetime
import json

def get_html(city,year,month):
    url = 'https://m.tianqi.com/lishi/%s/%s%s.html' % (city,year,month)
    request = urllib.request.Request(url)
    #设置请求头,避免发生403错误
    request.add_header('User-Agent','Mozilla/5.0')
    return urllib.request.urlopen(request).read().decode('UTF-8')
#print(get_html('guangzhou','2019','01'))

dates,temps = [] ,[]
city = 'hangzhou'
year ='2018'
months = ['%02d' % i for i in range(1,13)]
# months = ['01']

prev_day = datetime.datetime(2017,12,31)

for month in months:
    #下载网页源代码
    html = get_html(city,year,month)
    #将网页源代码的空格去掉
    nospace_text = ''.join(html.split())
    #定义包含全部天气数据的div元素对应的正则表达式,(.*?)代表非贪婪模式,也就是说只匹配符合条件的最少字符
    pattern = re.compile('(.*?)
') #返回根据正则表达式匹配到的字符串,是一个列表 div_list = re.findall(pattern,nospace_text) # print(div_list) pattern1 = re.compile('(.*?)') dls = re.findall(pattern1,div_list[0]) for dl in dls: #日期对应的正则表达式 date_pattern = re.compile('(.*?)') date_dd = re.findall(date_pattern,dl) #生成日期格式字符串 d_str = year + "/" + date_dd[0][0:5] #气温信息 temp_pattern = re.compile('(.*?)') temp_dd = re.findall(temp_pattern, dl) # print(temp_dd) temps.append(temp_dd) temps_value = [i for j in temps for i in j]; # print(temps_value) #统计各种天气的天数 days = {} for temp in temps_value: if temp not in days: days[temp] = 1 else: days[temp] += 1 # print(days) # 字典转换成json 存入本地文件 with open('days.json','w') as f: # 设置不转换成ascii json字符串首缩进 f.write(json.dumps(days,ensure_ascii=False,indent=2)) pie = pygal.Pie() other_num = 0 for i in days.keys(): if '雨' in i: other_num += days[i] else: pie.add(i,days[i]) pie.add('雨',other_num) pie.title = '杭州%s年的气温分析' % year pie.legend_at_buttom = True #输出图片 pie.render_to_file("tianqi.svg")

python+pygal分析杭州某年的天气_第1张图片

2、分析每种天气的情况

#coding:utf-8

import urllib.request,re
import pygal,datetime
import json

def get_html(city,year,month):
    url = 'https://m.tianqi.com/lishi/%s/%s%s.html' % (city,year,month)
    request = urllib.request.Request(url)
    #设置请求头,避免发生403错误
    request.add_header('User-Agent','Mozilla/5.0')
    return urllib.request.urlopen(request).read().decode('UTF-8')
#print(get_html('guangzhou','2019','01'))

dates,temps = [] ,[]
city = 'hangzhou'
year ='2018'
months = ['%02d' % i for i in range(1,13)]
# months = ['01']

prev_day = datetime.datetime(2017,12,31)

for month in months:
    #下载网页源代码
    html = get_html(city,year,month)
    #将网页源代码的空格去掉
    nospace_text = ''.join(html.split())
    #定义包含全部天气数据的div元素对应的正则表达式,(.*?)代表非贪婪模式,也就是说只匹配符合条件的最少字符
    pattern = re.compile('(.*?)
') #返回根据正则表达式匹配到的字符串,是一个列表 div_list = re.findall(pattern,nospace_text) # print(div_list) pattern1 = re.compile('(.*?)') dls = re.findall(pattern1,div_list[0]) for dl in dls: #日期对应的正则表达式 date_pattern = re.compile('(.*?)') date_dd = re.findall(date_pattern,dl) #生成日期格式字符串 d_str = year + "/" + date_dd[0][0:5] #气温信息 temp_pattern = re.compile('(.*?)') temp_dd = re.findall(temp_pattern, dl) # print(temp_dd) temps.append(temp_dd) temps_value = [i for j in temps for i in j]; # print(temps_value) #统计各种天气的天数 days = {} for temp in temps_value: if temp not in days: days[temp] = 1 else: days[temp] += 1 # print(days) # 字典转换成json 存入本地文件 with open('days.json','w') as f: # 设置不转换成ascii json字符串首缩进 f.write(json.dumps(days,ensure_ascii=False,indent=2)) pie = pygal.Pie() for j,k in days.items(): pie.add(j,k) pie.title = '杭州%s年的气温分析' % year pie.legend_at_buttom = True #输出图片 pie.render_to_file("tianqi.svg")

python+pygal分析杭州某年的天气_第2张图片

你可能感兴趣的:(python+pygal分析杭州某年的天气)