中国各省最高与最低气温热力图

1.天气数据获取

进入中国天气网,各省市天气被分为七个大区,将各个大区的URL放入一个列表,依次获取数据

urllist = ["http://www.weather.com.cn/textFC/hb.shtml",'http://www.weather.com.cn/textFC/db.shtml','http://www.weather.com.cn/textFC/hd.shtml',
       'http://www.weather.com.cn/textFC/hz.shtml','http://www.weather.com.cn/textFC/hn.shtml','http://www.weather.com.cn/textFC/xb.shtml',
       'http://www.weather.com.cn/textFC/xn.shtml']##需要爬取的七大片区链接,依次爬取
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41'}
for url in urllist:
    response = requests.get(url,headers=headers)##获得网页请求
    # print(response.content.decode('utf-8'))

    html=response.content.decode('utf-8')
    soup = BeautifulSoup(html,'lxml')##将获得的网页美化,方便获取所需数据
    all_weathers = soup.find('div', class_='hanml')  # 先找到最大的div
    weather=all_weathers.find_all('div', class_="conMidtab")[1]# 先找到天气的所有数据

为了方便起见,只需要获取每个省市的省会城市的数据,通过对页面结构的分析,获取我们需要的三个关键数据(城市,最高气温,最低气温) 

    for each_weather in weather.find_all('div', class_="conMidtab2"):
        all_tr_tag = each_weather.find_all('tr')  # 找到所有的tr标签
        # print(all_tr_tag[2].text)
        i=0
        for td in all_tr_tag[2].find_all('td'):  # 找到所有的td标签
            # print(td.text)
            # print(i)
            if i==0:     # 将数据存入对应列表
                print(td.text.strip('\n'))
                add.append(td.text.strip('\n'))
            if i==4:
                print(td.text)
                temp.append(td.text)
            if i==7:
                print(td.text)
                templow.append(td.text)
            i+=1

 

 2.数据可视化

map = Map('最高气温热力图',title_pos='center', width=1200, height=600)
map.add("", add, temp, visual_range=[0, 40], maptype='china', is_visualmap=True,is_label_show=True,
  visual_text_color='#000')

map2 = Map('最低气温热力图',title_pos='center', width=1200, height=600)
map2.add("", add, templow, visual_range=[0, 40], maptype='china', is_visualmap=True,is_label_show=True,
  visual_text_color='#000')

page = Page(page_title= "中国天气")  ##整合页面
page.add(map)
page.add(map2)
page.render("天气.html")

nowtime = time.localtime(time.time())
with open("天气.html", "r+", encoding='utf-8') as html:## 页面布局调整
    html_bf = BeautifulSoup(html, 'lxml')
    # print(html_bf)
    divs = html_bf.select('div')
    divs[0]['style'] = "width:700px;height:800px;position:absolute;top:5px;left:0px;border-style:solid;border-color:#444444;border-width:0px;"
    divs[1]["style"] = "width:700px;height:800px;position:absolute;top:5px;left:750px;border-style:solid;border-color:#444444;border-width:0px;"
    body = html_bf.find("body")
    div_title = "<div align=\"center\" style=\"width:1500px;\">\n<span style=\"font-size:32px;font face=\'黑体\';color:#000000\"><b>{}年{}月{}日 中国天气情况分析</b></div>".format(nowtime.tm_year,nowtime.tm_mon,nowtime.tm_mday)
    body["style"] = "background-color:#ffffff;"
    body.insert(0, BeautifulSoup(div_title, "lxml").div)
    html_new = str(html_bf)
    html.seek(0, 0)
    html.truncate()
    html.write(html_new)
    html.close()

3.最终效果

 

 

 

 

 

 

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要绘制近七天红河州各城市的气温热力图,我们可以使用Python的Matplotlib库和Seaborn库来完成。以下是实现的步骤: 1. 获取数据:我们可以从天气API接口或者爬虫获取近七天红河州各城市的气温数据。 2. 数据预处理:将获取的数据进行清洗和处理,将城市和气温分别存储在两个列表中。 3. 绘制热力图:使用Seaborn库的heatmap函数绘制热力图,其中x轴为城市,y轴为日期,颜色深浅表示气温的高低。 以下是代码示例: ```python import matplotlib.pyplot as plt import seaborn as sns import numpy as np # 获取数据并进行预处理 cities = ["红河", "个旧", "开远", "蒙自", "弥勒", "建水", "石屏", "元阳"] dates = ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07"] # 以下数据仅作示例,实际应替换为真实数据 temperatures = [[10, 12, 13, 15, 14, 11, 9, 8], [8, 11, 14, 16, 15, 12, 10, 7], [9, 12, 13, 15, 14, 11, 9, 8], [12, 13, 15, 17, 18, 14, 11, 10], [11, 13, 14, 16, 15, 12, 9, 8], [10, 12, 13, 15, 14, 11, 9, 8], [9, 11, 13, 15, 14, 11, 9, 7], [8, 10, 12, 14, 13, 10, 8, 6]] # 绘制热力图 sns.set() sns.set_style("whitegrid", {'grid.linestyle': '--'}) sns.set(font_scale=1.2) fig, ax = plt.subplots(figsize=(12, 8)) sns.heatmap(np.array(temperatures), annot=True, fmt=".1f", cmap="YlOrRd", cbar_kws={'label': 'Temperature(°C)'}, ax=ax) ax.set_xticklabels(cities, rotation=0, fontsize=14) ax.set_yticklabels(dates, rotation=0, fontsize=14) plt.title('Temperature Heatmap of Red River in the Past Seven Days', fontsize=16) plt.xlabel('City', fontsize=14) plt.ylabel('Date', fontsize=14) plt.show() ``` 注:以上代码仅为示例,实际应用中需要根据自己的数据进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Drunkpoem

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

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

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

打赏作者

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

抵扣说明:

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

余额充值