Python案例:查询城市天气并绘制最高气温与最低气温的折线图

该博客介绍了如何使用Python查询城市天气,并通过源代码展示如何绘制最高气温与最低气温的折线图。读者将学习到Python在数据获取和可视化方面的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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'] == 200:        
        weather['城市:'] = info['cityInfo']['parent'] + info['cityInfo'][
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值