【Python】python代码查询市级城市天气

本文介绍了一段Python代码,该代码能够查询市级城市的天气情况,弥补了大多数天气抓取工具缺乏查询功能的不足。此外,还提供了全国城市编号,以方便进行天气查询。运行此功能需要city.json文件。

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

python代码查询市级城市天气
加入了查询功能,我发现大多数天气抓取都没有查询功能,附加全国城市编号
运行需要city.json

*1. *[文件] CityWeather.py ~ 982B


# -*- coding: cp936 -*-
 
import re
import urllib2
import json
import string
 
of = open('city.json','r')
page = of.read()
of.close()
 
key = raw_input('请输入要查询天气的城市名: ')
r = '''"市名": "%s",
                    "编码": "(.+?)"''' %key
s = re.findall(r,page,re.S)
s = s[0]
 
#API更新,旧API时间错误注释掉
#url ='http://m.weather.com.cn/data/%s.html' %s
url ='http://m.weather.com.cn/atad/%s.html' %s
re = urllib2.urlopen(url).read()
we = json.loads(re)['weatherinfo']
print we['city'] , we['date_y'].center(30) ,   we['week']
print we['temp1'], we['weather1'].center(30),  we['wind1']
print we['temp2'], we['weather2'].center(30),  we['wind2']
print we['temp3'], we['weather3'].center(30),  we['wind3']
print we['temp4'], we['weather4'].center(30),  we['wind4']
print we['temp5'], we['weather5'].center(30),  we['wind5']
print we['temp6'], we['weather6'].center(30),  we['wind6']
print we['index48_d']
raw_input('plese input anykey to esc')

  1. [代码][Python]代码

{
    "城市代码": [
        {
            "省": "北京",
            "市": [
                {
                    "市名": "北京",
                    "编码": "101010100"
                },
                {
                    "市名": "朝阳",
                    "编码": "101010300"
                },
                {
                    "市名": "顺义",
                    "编码": "101010400"
                },
                {
                    "市名": "怀柔",
                    "编码": "101010500"
                },
                {
                    "市名": "通州",
                    "编码": "101010600"
                },
                {
                    "市名": "昌平",
                    "编码": "101010700"
                },
                {
                    "市名": "延庆",
                    "编码": "101010800"
                },
                {
                    "市名": "丰台",
                    "编码": "101010900"
                },
                {
                    "市名": "石景山",
                    "编码": "101011000"
                },
                {
                    "市名": "大兴",
                    "编码": "101011100"
                },
                {
                    "市名": "房山",
                    "编码": "101011200"
                },
                {
                    "市名": "密云",
                    "编码": "101011300"
                },
                {
                    "市名": "门头沟",
                    "编码": "101011400"
                },
                {
                    "市名": "平谷",
                    "编码": "101011500"
                },
                {
                    "市名": "八达岭",
                    "编码": "101011600"
                },
                {
                     
2000
"市名": "佛爷顶",
                    "编码": "101011700"
                },
                {
                    "市名": "汤河口",
                    "编码": "101011800"
                },
                {
                    "市名": "密云上甸子",
                    "编码": "101011900"
                },
                {
                    "市名": "斋堂",
                    "编码": "101012000"
                },
                {
                    "市名": "霞云岭",
                    "编码": "101012100"
                },
                {
                    "市名": "北京城区",
                    "编码": "101012200"
                },
                {
                    "市名": "海淀",
                    "编码": "101010200"
                }
            ]
        },
        {
            "省": "天津市",
            "市": [
                {
                    "市名": "天津",
                    "编码": "101030100"
                },
                {
                    "市名": "宝坻",
                    "编码": "101030300"
                },
                {
                    "市名": "东丽",
                    "编码": "101030400"
                },
                {
                    "市名": "西青",
                    "编码": "101030500"
                },
                {
                    "市名": "北辰",
                    "编码": "101030600"
                },
                {
                    "市名": "蓟县",
                    "编码": "101031400"
                },
                {
                    "市名": "汉沽",
                    "编码": "101030800"
                },
                {
                    "市名": "静海",
                    "编码": "101030900"
                },
                {
                    "市名": "津南",
                    "编码": "101031000"
                },
                {
                    "市名": "塘沽",
                    "编码": "101031100"
                },
                {
                    "市名": "大港",
                    "编码": "101031200"
                },
                {
                    "市名": "武清",
                    "编码": "101030200"
                },
                {
                    "市名": "宁河",
                    "编码": "101030700"
                }
            ]
        },
        {
            "省": "上海",
            "市": [
                {
                    "市名": "上海",
                    "编码": "101020100"
                },
                {
                    "市名": "宝山",
                    "编码": "101020300"
                },
                {
                    "市名": "嘉定",
                    "编码": "101020500"
                },
                {
                    "市名": "南汇",
                    "编码": "101020600"
                },
                {
                    "市名": "浦东",
                    "编码": "101021300"
                },
                {
                    "市名": "青浦",
                    "编码": "101020800"
                },
                {
                    "市名": "松江",
                    "编码": "101020900"
                },
                {
                    "市名": "奉贤",
                    "编码": "101021000"
                },
                {
                    "市名": "崇明",
                    "编码": "101021100"
                },
                {
                    "市名": "徐家汇",
                    "编码": "101021200"
                },
                {
                    "市名": "闵行",
                    "编码": "101020200"
                },
                {
                    "市名": "金山",
                    "编码": "101020700"
                }
            ]
        },
        {
            "省": "河北",
            "市": [
                {
                    "市名": "石家庄",
                    "编码": "101090101"
                },
                {
                    "市名": "张家口",
                    "编码": "101090301"
                },
                {
                    "市名": "承德",
                    "编码": "101090402"
                },
                {
                    "市名": "唐山",
                    "编码": "101090501"
                },
                {
                    "市名": "秦皇岛",
                    "编码": "101091101"
                },
                {
                    "市名": "沧州",
                    "编码": "101090701"
                },
                {
                    "市名": "衡水",
                    "编码": "101090801"
                },
                {
                    "市名": "邢台",
                    "编码": "101090901"
                },
                {
                    "市名": "邯郸",
                    "编码": "101091001"
                },
                {
                    "市名": "保定",
                    "编码": "101090201"
                },
                {
                    "市名": "廊坊",
                    "编码": "101090601"
                }
            ]
        },
        {
            "省": "河南",
            "市": [
                {
                    "市名": "郑州",
                    "编码": "101180101"
                },
                {
                    "市名": "新乡",
                    "编码": "101180301"
                },
                {
                    "市名": "许昌",
                    "编码": "101180401"
                },
                {
                    "市名": "平顶山",
                    "编码": "101180501"
                },
                {
                    "市名": "信阳",
                    "编码": "101180601"
                },
                {
                    "市名": "南阳",
                    "编码": "101180701"
                },
                {
                    "市名": "开封",
                    "编码": "101180801"
                },
                {
                    "市名": "洛阳",
                    "编码": "101180901"
                },
                {
                    "市名": "商丘",
                    "编码": "101181001"
                },
                {
                    "市名": "焦作",
                    "编码": "101181101"
                },
                {
                    "市名": "鹤壁",
                    "编码": "101181201"
                },
                {
                    "市名": "濮阳",
                    "编码": "101181301"
                },
                {
                    "市名": "周口",
                    "编码": "101181401"
                },
                {
                    "市名": "漯河",
                    "编码": "101181501"
                },
                {
                    "市名": "驻马店",
                    "编码": "101181601"
                },
                {
                    "市名": "三门峡",
                    "编码": "101181701"
                },
                {
                    "市名": "济源",
                    "编码": "101181801"
                },
                {
                    "市名": "安阳",
                    "编码": "101180201"
                }
            ]
        },
        {
            "省": "安徽",
            "市": [
                {
                    "市名": "合肥",
                    "编码": "101220101"
                },
                {
                    "市名": "芜湖",
                    "编码": "101220301"
          
2000
       },
                {
                    "市名": "淮南",
                    "编码": "101220401"
                },
                {
                    "市名": "马鞍山",
                    "编码": "101220501"
                },
                {
                    "市名": "安庆",
                   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值