python制作气温分布图_Python案例:查询城市天气并绘制最高气温与最低气温的折线图...

该博客介绍如何使用Python查询城市天气信息,并通过绘制折线图展示未来几天的最高气温与最低气温。首先,从天气API获取城市代码和天气预报数据,然后解析数据并打印天气详情。接着,提取气温数据并绘制折线图。最后,提供了用户输入城市名运行示例。
摘要由CSDN通过智能技术生成

1、编写源代码 - 查询城市天气.

# -*- coding: utf-8 -*-

"""

Spyder Editor

This is a temporary script file.

"""

# 功能:查询城市天气

import requests, json, re

from matplotlib import pyplot as plt

# 获取城市代码

def getCityCode(city):

url = 'http://toy1.weather.com.cn/search?cityname=' + city

r = requests.get(url)

if len(r.text) > 4:

json_arr = json.loads(r.text[1:len(r.text)-1])

code = json_arr[0]['ref'][0:9]

return code

else:

return "000000000"

# 获取城市天气信息

def getWeatherInfo(city):

code = getCityCode(city)

url = 'http://t.weather.sojson.com/api/weather/city/' + code

r = requests.get(url)

info = r.json()

weather = {}

if info['status'

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值