Python:查找天气预报json接口的城市代码(A)

天气预报json接口当需要获取某个城市天气数据json时候,需要传入一个城市代码编码作为入参,例如 http://t.weather.itboy.net/api/weather/city/xxxxxx ,其中xxxxxx即为城市代码,以下是一段python代码,提供一个简单查询函数,输入中文城市名,返回该城市的城市代码。

import pandas as pd

def search_city_code(city_name):
    city_code = '-1'

    df = pd.read_json('city_code.json')
    for row in df.values:
        row_data = row[0]
        city_data = row_data['市']
        for cd in city_data:
            # print(cd['市名'], cd['编码'])
            if (city_name == cd['市名'] or cd['市名'] in city_name):
                city_code = cd['编码']
                break

    return city_code


if __name__ == '__main__':
    print(search_city_code('成都'))

 

以下市天气预报json数据获取中使用的城市代码:

{
	"城市代码": [{
			"省": "北京",
			"市": [{
					"市名": "北京",
					"编码": "101010100"
				},
				{
					"市名": "朝阳",
					"编码": "101010300"
				},
				{
					"市名": "顺义",
					"编码": "101010400"
				},
				{
					"市名": "怀柔",
					"编码": "101010500"
				},
				{
					"市名": "通州",
					"编码": "101010600"
				},
				{
					"市名": "昌平",
					"编码": "101010700"
				},
				{
					"市名": "延庆",
					"编码": "101010800"
				},
				{
					"市名": "丰台",
					"编码": "101010900"
				},
				{
					"市名": "石景山",
					"编码": "101011000"
				},
				{
					"市名": "大兴",
					"编码": "101011100"
				},
				{
					"市名": "房山",
					"编码": "101011200"
				},
				{
					"市名": "密云",
					"编码": "101011300"
				},
				{
					"市名": "门头沟",
					"编码": "101011400"
				},
				{
					"市名": "平谷",
					"编码": "101011500"
				},
				{
					"市名": "八达岭",
					"编码": "101011600"
				},
				{
					"市名": "佛爷顶",
					"编码": "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"
				},
				{
					"市名": "淮南",
					"编码": "101220401"
				},
				{
					"市名": "马鞍山",
					"编码": "101220501"
				},
				{
					"市名": "安庆",
					"编码": "101220601"
				},
				{
					"市名": "宿州",
					"编码": "101220701"
				},
				{
					"市名": "阜阳",
					"编码": "101220801"
				},
				{
					"市名": "亳州",
					"编码": "101220901"
				},
				{
					"市名": "黄山",
					"编码": "101221001"
				},
				{
					"市名": "滁州",
					"编码": "101221101"
				},
				{
					"市名": "淮北",
					"编码": "101221201"
				},
				{
					"市名": "铜陵",
					"编码": "101221301"
				},
				{
					"市名": "宣城",
					"编码": "101221401"
				},
				{
					"市名": "六安",
					"编码": "101221501"
				},
				{
					"市名": "巢湖",
					"编码": "101221601"
				},
				{
					"市名": "池州",
					"编码": "101221701"
				},
				{
					"市名": "蚌埠",
					"编码": "101220201"
				}
			]
		},
		{
			"省": "浙江",
			"市": [{
					"市名": "杭州",
					"编码": "101210101"
				},
				{
					"市名": "舟山",
					"编码": "101211101"
				},
				{
					"市名": "湖州",
					"编码": "101210201"
				},
				{
					"市名": "嘉兴",
					"编码": "101210301"
				},
				{
					"市名": "金华",
					"编码": "101210901"
				},
				{
					"市名": "绍兴",
					"编码": "101210501"
				},
				{
					"市名": "台州",
					"编码": "101210601"
				},
				{
					"市名": "温州",
					"编码": "101210701"
				},
				{
					"市名": "丽水",
					"编码": "101210801"
				},
				{
					"市名": "衢州",
					"编码": "101211001"
				},
				{
					"市名": "宁波",
					"编码": "101210401"
				}
			]
		},
		{
			"省": "重庆",
			"市": [{
					"市名": "重庆",
					"编码": "101040100"
				},
				{
					"市名": "合川",
					"编码": "101040300"
				},
				{
					"市名": "南川",
					"编码": "101040400"
				},
				{
					"市名": "江津",
					"编码": "101040500"
				},
				{
					"市名": "万盛",
					"编码": "101040600"
				},
				{
					"市名": "渝北",
					"编码": "101040700"
				},
				{
					"市名": "北碚",
					"编码": "101040800"
				},
				{
					"市名": "巴南",
					"编码": "101040900"
				},
				{
					"市名": "长寿",
					"编码": "101041000"
				},
				{
					"市名": "黔江",
					"编码": "101041100"
				},
				{
					"市名": "万州天城",
					"编码": "101041200"
				},
				{
					"市名": "万州龙宝",
					"编码": "101041300"
				},
				{
					"市名": "涪陵",
					"编码": "101041400"
				},
				{
					"市名": "开县",
					"编码": "101041500"
				},
				{
					"市名": "城口",
					"编码": "101041600"
				},
				{
					"市名": "云阳",
					"编码": "101041700"
				},
				{
					"市名": "巫溪",
					"编码": "101041800"
				},
				{
					"市名": "奉节",
					"编码": "101041900"
				},
				{
					"市名": "巫山",
					"编码": "101042000"
				},
				{
					"市名": "潼南",
					"编码": "101042100"
				},
				{
					"市名": "垫江",
					"编码": "101042200"
				},
				{
					"市名": "梁平",
					"编码": "101042300"
				},
				{
					"市名": "忠县",
					"编码": "101042400"
				},
				{
					"市名": "石柱",
					"编码": "101042500"
				},
				{
					"市名": "大足",
					"编码": "101042600"
				},
				{
					"市名": "荣昌",
					"编码": "101042700"
				},
				{
					"市名": "铜梁",
					"编码": "101042800"
				},
				{
					"市名": "璧山",
					"编码": "101042900"
				},
				{
					"市名": "丰都",
					"编码": "101043000"
				},
				{
					"市名": "武隆",
					"编码": "101043100"
				},
				{
					"市名": "彭水",
					"编码": "101043200"
				},
				{
					"市名": "綦江",
					"编码": "101043300"
				},
				{
					"市名": "酉阳",
					"编码": "101043400"
				},
				{
					"市名": "秀山",
					"编码": "101043600"
				},
				{
					"市名": "沙坪坝",
					"编码": "101043700"
				},
				{
					"市名": "永川",
					"编码": "101040200"
				}
			]
		},
		{
			"省": "福建",
			"市": [{
					"市名": "福州",
					"编码": "101230101"
				},
				{
					"市名": "泉州",
					"编码": "101230501"
				},
				{
					"市名": "漳州",
					"编码": "101230601"
				},
				{
					"市名": "龙岩",
					"编码": "101230701"
				},
				{
					"市名": "晋江",
					"编码": "101230509"
				},
				{
					"市名": "南平",
					"编码": "101230901"
				},
				{
					"市名": "厦门",
					"编码": "101230201"
				},
				{
					"市名": "宁德",
					"编码": "101230301"
				},
				{
					"市名": "莆田",
					"编码": "101230401"
				},
				{
					"市名": "三明",
					"编码": "101230801"
				}
			]
		},
		{
			"省": "甘肃",
			"市": [{
					"市名": "兰州",
					"编码": "101160101"
				},
				{
					"市名": "平凉",
					"编码": "101160301"
				},
				{
					"市名": "庆阳",
					"编码": "101160401"
				},
				{
					"市名": "武威",
					"编码": "101160501"
				},
				{
					"市名": "金昌",
					"编码": "101160601"
				},
				{
					"市名": "嘉峪关",
					"编码": "101161401"
				},
				{
					"市名": "酒泉",
					"编码": "101160801"
				},
				{
					"市名": "天水",
					"编码": "101160901"
				},
				{
					"市名": "武都",
					"编码": "101161001"
				},
				{
					"市名": "临夏",
					"编码": "101161101"
				},
				{
					"市名": "合作",
					"编码": "101161201"
				},
				{
					"市名": "白银",
					"编码": "101161301"
				},
				{
					"市名": "定西",
					"编码": "101160201"
				},
				{
					"市名": "张掖",
					"编码": "101160701"
				}
			]
		},
		{
			"省": "广东",
			"市": [{
					"市名": "广州",
					"编码": "101280101"
				},
				{
					"市名": "惠州",
					"编码": "101280301"
				},
				{
					"市名": "梅州",
					"编码": "101280401"
				},
				{
					"市名": "汕头",
					"编码": "101280501"
				},
				{
					"市名": "深圳",
					"编码": "101280601"
				},
				{
					"市名": "珠海",
					"编码": "101280701"
				},
				{
					"市名": "佛山",
					"编码": "101280800"
				},
				{
					"市名": "肇庆",
					"编码": "101280901"
				},
				{
					"市名": "湛江",
					"编码": "101281001"
				},
				{
					"市名": "江门",
					"编码": "101281101"
				},
				{
					"市名": "河源",
					"编码": "101281201"
				},
				{
					"市名": "清远",
					"编码": "101281301"
				},
				{
					"市名": "云浮",
					"编码": "101281401"
				},
				{
					"市名": "潮州",
					"编码": "101281501"
				},
				{
					"市名": "东莞",
					"编码": "101281601"
				},
				{
					"市名": "中山",
					"编码": "101281701"
				},
				{
					"市名": "阳江",
					"编码": "101281801"
				},
				{
					"市名": "揭阳",
					"编码": "101281901"
				},
				{
					"市名": "茂名",
					"编码": "101282001"
				},
				{
					"市名": "汕尾",
					"编码": "101282101"
				},
				{
					"市名": "韶关",
					"编码": "101280201"
				}
			]
		},
		{
			"省": "广西",
			"市": [{
					"市名": "南宁",
					"编码": "101300101"
				},
				{
					"市名": "柳州",
					"编码": "101300301"
				},
				{
					"市名": "来宾",
					"编码": "101300401"
				},
				{
					"市名": "桂林",
					"编码": "101300501"
				},
				{
					"市名": "梧州",
					"编码": "101300601"
				},
				{
					"市名": "防城港",
					"编码": "101301401"
				},
				{
					"市名": "贵港",
					"编码": "101300801"
				},
				{
					"市名": "玉林",
					"编码": "101300901"
				},
				{
					"市名": "百色",
					"编码": "101301001"
				},
				{
					"市名": "钦州",
					"编码": "101301101"
				},
				{
					"市名": "河池",
					"编码": "101301201"
				},
				{
					"市名": "北海",
					"编码": "101301301"
				},
				{
					"市名": "崇左",
					"编码": "101300201"
				},
				{
					"市名": "贺州",
					"编码": "101300701"
				}
			]
		},
		{
			"省": "贵州",
			"市": [{
					"市名": "贵阳",
					"编码": "101260101"
				},
				{
					"市名": "安顺",
					"编码": "101260301"
				},
				{
					"市名": "都匀",
					"编码": "101260401"
				},
				{
					"市名": "兴义",
					"编码": "101260906"
				},
				{
					"市名": "铜仁",
					"编码": "101260601"
				},
				{
					"市名": "毕节",
					"编码": "101260701"
				},
				{
					"市名": "六盘水",
					"编码": "101260801"
				},
				{
					"市名": "遵义",
					"编码": "101260201"
				},
				{
					"市名": "凯里",
					"编码": "101260501"
				}
			]
		},
		{
			"省": "云南",
			"市": [{
					"市名": "昆明",
					"编码": "101290101"
				},
				{
					"市名": "红河",
					"编码": "101290301"
				},
				{
					"市名": "文山",
					"编码": "101290601"
				},
				{
					"市名": "玉溪",
					"编码": "101290701"
				},
				{
					"市名": "楚雄",
					"编码": "101290801"
				},
				{
					"市名": "普洱",
					"编码": "101290901"
				},
				{
					"市名": "昭通",
					"编码": "101291001"
				},
				{
					"市名": "临沧",
					"编码": "101291101"
				},
				{
					"市名": "怒江",
					"编码": "101291201"
				},
				{
					"市名": "香格里拉",
					"编码": "101291301"
				},
				{
					"市名": "丽江",
					"编码": "101291401"
				},
				{
					"市名": "德宏",
					"编码": "101291501"
				},
				{
					"市名": "景洪",
					"编码": "101291601"
				},
				{
					"市名": "大理",
					"编码": "101290201"
				},
				{
					"市名": "曲靖",
					"编码": "101290401"
				},
				{
					"市名": "保山",
					"编码": "101290501"
				}
			]
		},
		{
			"省": "内蒙古",
			"市": [{
					"市名": "呼和浩特",
					"编码": "101080101"
				},
				{
					"市名": "乌海",
					"编码": "101080301"
				},
				{
					"市名": "集宁",
					"编码": "101080401"
				},
				{
					"市名": "通辽",
					"编码": "101080501"
				},
				{
					"市名": "阿拉善左旗",
					"编码": "101081201"
				},
				{
					"市名": "鄂尔多斯",
					"编码": "101080701"
				},
				{
					"市名": "临河",
					"编码": "101080801"
				},
				{
					"市名": "锡林浩特",
					"编码": "101080901"
				},
				{
					"市名": "呼伦贝尔",
					"编码": "101081000"
				},
				{
					"市名": "乌兰浩特",
					"编码": "101081101"
				},
				{
					"市名": "包头",
					"编码": "101080201"
				},
				{
					"市名": "赤峰",
					"编码": "101080601"
				}
			]
		},
		{
			"省": "江西",
			"市": [{
					"市名": "南昌",
					"编码": "101240101"
				},
				{
					"市名": "上饶",
					"编码": "101240301"
				},
				{
					"市名": "抚州",
					"编码": "101240401"
				},
				{
					"市名": "宜春",
					"编码": "101240501"
				},
				{
					"市名": "鹰潭",
					"编码": "101241101"
				},
				{
					"市名": "赣州",
					"编码": "101240701"
				},
				{
					"市名": "景德镇",
					"编码": "101240801"
				},
				{
					"市名": "萍乡",
					"编码": "101240901"
				},
				{
					"市名": "新余",
					"编码": "101241001"
				},
				{
					"市名": "九江",
					"编码": "101240201"
				},
				{
					"市名": "吉安",
					"编码": "101240601"
				}
			]
		},
		{
			"省": "湖北",
			"市": [{
					"市名": "武汉",
					"编码": "101200101"
				},
				{
					"市名": "黄冈",
					"编码": "101200501"
				},
				{
					"市名": "荆州",
					"编码": "101200801"
				},
				{
					"市名": "宜昌",
					"编码": "101200901"
				},
				{
					"市名": "恩施",
					"编码": "101201001"
				},
				{
					"市名": "十堰",
					"编码": "101201101"
				},
				{
					"市名": "神农架",
					"编码": "101201201"
				},
				{
					"市名": "随州",
					"编码": "101201301"
				},
				{
					"市名": "荆门",
					"编码": "101201401"
				},
				{
					"市名": "天门",
					"编码": "101201501"
				},
				{
					"市名": "仙桃",
					"编码": "101201601"
				},
				{
					"市名": "潜江",
					"编码": "101201701"
				},
				{
					"市名": "襄樊",
					"编码": "101200201"
				},
				{
					"市名": "鄂州",
					"编码": "101200301"
				},
				{
					"市名": "孝感",
					"编码": "101200401"
				},
				{
					"市名": "黄石",
					"编码": "101200601"
				},
				{
					"市名": "咸宁",
					"编码": "101200701"
				}
			]
		},
		{
			"省": "四川",
			"市": [{
					"市名": "成都",
					"编码": "101270101"
				},
				{
					"市名": "自贡",
					"编码": "101270301"
				},
				{
					"市名": "绵阳",
					"编码": "101270401"
				},
				{
					"市名": "南充",
					"编码": "101270501"
				},
				{
					"市名": "达州",
					"编码": "101270601"
				},
				{
					"市名": "遂宁",
					"编码": "101270701"
				},
				{
					"市名": "广安",
					"编码": "101270801"
				},
				{
					"市名": "巴中",
					"编码": "101270901"
				},
				{
					"市名": "泸州",
					"编码": "101271001"
				},
				{
					"市名": "宜宾",
					"编码": "101271101"
				},
				{
					"市名": "内江",
					"编码": "101271201"
				},
				{
					"市名": "资阳",
					"编码": "101271301"
				},
				{
					"市名": "乐山",
					"编码": "101271401"
				},
				{
					"市名": "眉山",
					"编码": "101271501"
				},
				{
					"市名": "凉山",
					"编码": "101271601"
				},
				{
					"市名": "雅安",
					"编码": "101271701"
				},
				{
					"市名": "甘孜",
					"编码": "101271801"
				},
				{
					"市名": "阿坝",
					"编码": "101271901"
				},
				{
					"市名": "德阳",
					"编码": "101272001"
				},
				{
					"市名": "广元",
					"编码": "101272101"
				},
				{
					"市名": "攀枝花",
					"编码": "101270201"
				}
			]
		},
		{
			"省": "宁夏",
			"市": [{
					"市名": "银川",
					"编码": "101170101"
				},
				{
					"市名": "中卫",
					"编码": "101170501"
				},
				{
					"市名": "固原",
					"编码": "101170401"
				},
				{
					"市名": "石嘴山",
					"编码": "101170201"
				},
				{
					"市名": "吴忠",
					"编码": "101170301"
				}
			]
		},
		{
			"省": "青海省",
			"市": [{
					"市名": "西宁",
					"编码": "101150101"
				},
				{
					"市名": "黄南",
					"编码": "101150301"
				},
				{
					"市名": "海北",
					"编码": "101150801"
				},
				{
					"市名": "果洛",
					"编码": "101150501"
				},
				{
					"市名": "玉树",
					"编码": "101150601"
				},
				{
					"市名": "海西",
					"编码": "101150701"
				},
				{
					"市名": "海东",
					"编码": "101150201"
				},
				{
					"市名": "海南",
					"编码": "101150401"
				}
			]
		},
		{
			"省": "山东",
			"市": [{
					"市名": "济南",
					"编码": "101120101"
				},
				{
					"市名": "潍坊",
					"编码": "101120601"
				},
				{
					"市名": "临沂",
					"编码": "101120901"
				},
				{
					"市名": "菏泽",
					"编码": "101121001"
				},
				{
					"市名": "滨州",
					"编码": "101121101"
				},
				{
					"市名": "东营",
					"编码": "101121201"
				},
				{
					"市名": "威海",
					"编码": "101121301"
				},
				{
					"市名": "枣庄",
					"编码": "101121401"
				},
				{
					"市名": "日照",
					"编码": "101121501"
				},
				{
					"市名": "莱芜",
					"编码": "101121601"
				},
				{
					"市名": "聊城",
					"编码": "101121701"
				},
				{
					"市名": "青岛",
					"编码": "101120201"
				},
				{
					"市名": "淄博",
					"编码": "101120301"
				},
				{
					"市名": "德州",
					"编码": "101120401"
				},
				{
					"市名": "烟台",
					"编码": "101120501"
				},
				{
					"市名": "济宁",
					"编码": "101120701"
				},
				{
					"市名": "泰安",
					"编码": "101120801"
				}
			]
		},
		{
			"省": "陕西省",
			"市": [{
					"市名": "西安",
					"编码": "101110101"
				},
				{
					"市名": "延安",
					"编码": "101110300"
				},
				{
					"市名": "榆林",
					"编码": "101110401"
				},
				{
					"市名": "铜川",
					"编码": "101111001"
				},
				{
					"市名": "商洛",
					"编码": "101110601"
				},
				{
					"市名": "安康",
					"编码": "101110701"
				},
				{
					"市名": "汉中",
					"编码": "101110801"
				},
				{
					"市名": "宝鸡",
					"编码": "101110901"
				},
				{
					"市名": "咸阳",
					"编码": "101110200"
				},
				{
					"市名": "渭南",
					"编码": "101110501"
				}
			]
		},
		{
			"省": "山西",
			"市": [{
					"市名": "太原",
					"编码": "101100101"
				},
				{
					"市名": "临汾",
					"编码": "101100701"
				},
				{
					"市名": "运城",
					"编码": "101100801"
				},
				{
					"市名": "朔州",
					"编码": "101100901"
				},
				{
					"市名": "忻州",
					"编码": "101101001"
				},
				{
					"市名": "长治",
					"编码": "101100501"
				},
				{
					"市名": "大同",
					"编码": "101100201"
				},
				{
					"市名": "阳泉",
					"编码": "101100301"
				},
				{
					"市名": "晋中",
					"编码": "101100401"
				},
				{
					"市名": "晋城",
					"编码": "101100601"
				},
				{
					"市名": "吕梁",
					"编码": "101101100"
				}
			]
		},
		{
			"省": "新疆",
			"市": [{
					"市名": "乌鲁木齐",
					"编码": "101130101"
				},
				{
					"市名": "石河子",
					"编码": "101130301"
				},
				{
					"市名": "昌吉",
					"编码": "101130401"
				},
				{
					"市名": "吐鲁番",
					"编码": "101130501"
				},
				{
					"市名": "库尔勒",
					"编码": "101130601"
				},
				{
					"市名": "阿拉尔",
					"编码": "101130701"
				},
				{
					"市名": "阿克苏",
					"编码": "101130801"
				},
				{
					"市名": "喀什",
					"编码": "101130901"
				},
				{
					"市名": "伊宁",
					"编码": "101131001"
				},
				{
					"市名": "塔城",
					"编码": "101131101"
				},
				{
					"市名": "哈密",
					"编码": "101131201"
				},
				{
					"市名": "和田",
					"编码": "101131301"
				},
				{
					"市名": "阿勒泰",
					"编码": "101131401"
				},
				{
					"市名": "阿图什",
					"编码": "101131501"
				},
				{
					"市名": "博乐",
					"编码": "101131601"
				},
				{
					"市名": "克拉玛依",
					"编码": "101130201"
				}
			]
		},
		{
			"省": "西藏",
			"市": [{
					"市名": "拉萨",
					"编码": "101140101"
				},
				{
					"市名": "山南",
					"编码": "101140301"
				},
				{
					"市名": "阿里",
					"编码": "101140701"
				},
				{
					"市名": "昌都",
					"编码": "101140501"
				},
				{
					"市名": "那曲",
					"编码": "101140601"
				},
				{
					"市名": "日喀则",
					"编码": "101140201"
				},
				{
					"市名": "林芝",
					"编码": "101140401"
				}
			]
		},
		{
			"省": "台湾",
			"市": [{
					"市名": "台北县",
					"编码": "101340101"
				},
				{
					"市名": "高雄",
					"编码": "101340201"
				},
				{
					"市名": "台中",
					"编码": "101340401"
				}
			]
		},
		{
			"省": "海南省",
			"市": [{
					"市名": "海口",
					"编码": "101310101"
				},
				{
					"市名": "三亚",
					"编码": "101310201"
				},
				{
					"市名": "东方",
					"编码": "101310202"
				},
				{
					"市名": "临高",
					"编码": "101310203"
				},
				{
					"市名": "澄迈",
					"编码": "101310204"
				},
				{
					"市名": "儋州",
					"编码": "101310205"
				},
				{
					"市名": "昌江",
					"编码": "101310206"
				},
				{
					"市名": "白沙",
					"编码": "101310207"
				},
				{
					"市名": "琼中",
					"编码": "101310208"
				},
				{
					"市名": "定安",
					"编码": "101310209"
				},
				{
					"市名": "屯昌",
					"编码": "101310210"
				},
				{
					"市名": "琼海",
					"编码": "101310211"
				},
				{
					"市名": "文昌",
					"编码": "101310212"
				},
				{
					"市名": "保亭",
					"编码": "101310214"
				},
				{
					"市名": "万宁",
					"编码": "101310215"
				},
				{
					"市名": "陵水",
					"编码": "101310216"
				},
				{
					"市名": "西沙",
					"编码": "101310217"
				},
				{
					"市名": "南沙岛",
					"编码": "101310220"
				},
				{
					"市名": "乐东",
					"编码": "101310221"
				},
				{
					"市名": "五指山",
					"编码": "101310222"
				},
				{
					"市名": "琼山",
					"编码": "101310102"
				}
			]
		},
		{
			"省": "湖南",
			"市": [{
					"市名": "长沙",
					"编码": "101250101"
				},
				{
					"市名": "株洲",
					"编码": "101250301"
				},
				{
					"市名": "衡阳",
					"编码": "101250401"
				},
				{
					"市名": "郴州",
					"编码": "101250501"
				},
				{
					"市名": "常德",
					"编码": "101250601"
				},
				{
					"市名": "益阳",
					"编码": "101250700"
				},
				{
					"市名": "娄底",
					"编码": "101250801"
				},
				{
					"市名": "邵阳",
					"编码": "101250901"
				},
				{
					"市名": "岳阳",
					"编码": "101251001"
				},
				{
					"市名": "张家界",
					"编码": "101251101"
				},
				{
					"市名": "怀化",
					"编码": "101251201"
				},
				{
					"市名": "黔阳",
					"编码": "101251301"
				},
				{
					"市名": "永州",
					"编码": "101251401"
				},
				{
					"市名": "吉首",
					"编码": "101251501"
				},
				{
					"市名": "湘潭",
					"编码": "101250201"
				}
			]
		},
		{
			"省": "江苏",
			"市": [{
					"市名": "南京",
					"编码": "101190101"
				},
				{
					"市名": "镇江",
					"编码": "101190301"
				},
				{
					"市名": "苏州",
					"编码": "101190401"
				},
				{
					"市名": "南通",
					"编码": "101190501"
				},
				{
					"市名": "扬州",
					"编码": "101190601"
				},
				{
					"市名": "宿迁",
					"编码": "101191301"
				},
				{
					"市名": "徐州",
					"编码": "101190801"
				},
				{
					"市名": "淮安",
					"编码": "101190901"
				},
				{
					"市名": "连云港",
					"编码": "101191001"
				},
				{
					"市名": "常州",
					"编码": "101191101"
				},
				{
					"市名": "泰州",
					"编码": "101191201"
				},
				{
					"市名": "无锡",
					"编码": "101190201"
				},
				{
					"市名": "盐城",
					"编码": "101190701"
				}
			]
		},
		{
			"省": "黑龙江",
			"市": [{
					"市名": "哈尔滨",
					"编码": "101050101"
				},
				{
					"市名": "牡丹江",
					"编码": "101050301"
				},
				{
					"市名": "佳木斯",
					"编码": "101050401"
				},
				{
					"市名": "绥化",
					"编码": "101050501"
				},
				{
					"市名": "黑河",
					"编码": "101050601"
				},
				{
					"市名": "双鸭山",
					"编码": "101051301"
				},
				{
					"市名": "伊春",
					"编码": "101050801"
				},
				{
					"市名": "大庆",
					"编码": "101050901"
				},
				{
					"市名": "七台河",
					"编码": "101051002"
				},
				{
					"市名": "鸡西",
					"编码": "101051101"
				},
				{
					"市名": "鹤岗",
					"编码": "101051201"
				},
				{
					"市名": "齐齐哈尔",
					"编码": "101050201"
				},
				{
					"市名": "大兴安岭",
					"编码": "101050701"
				}
			]
		},
		{
			"省": "吉林",
			"市": [{
					"市名": "长春",
					"编码": "101060101"
				},
				{
					"市名": "延吉",
					"编码": "101060301"
				},
				{
					"市名": "四平",
					"编码": "101060401"
				},
				{
					"市名": "白山",
					"编码": "101060901"
				},
				{
					"市名": "白城",
					"编码": "101060601"
				},
				{
					"市名": "辽源",
					"编码": "101060701"
				},
				{
					"市名": "松原",
					"编码": "101060801"
				},
				{
					"市名": "吉林",
					"编码": "101060201"
				},
				{
					"市名": "通化",
					"编码": "101060501"
				}
			]
		},
		{
			"省": "辽宁",
			"市": [{
					"市名": "沈阳",
					"编码": "101070101"
				},
				{
					"市名": "鞍山",
					"编码": "101070301"
				},
				{
					"市名": "抚顺",
					"编码": "101070401"
				},
				{
					"市名": "本溪",
					"编码": "101070501"
				},
				{
					"市名": "丹东",
					"编码": "101070601"
				},
				{
					"市名": "葫芦岛",
					"编码": "101071401"
				},
				{
					"市名": "营口",
					"编码": "101070801"
				},
				{
					"市名": "阜新",
					"编码": "101070901"
				},
				{
					"市名": "辽阳",
					"编码": "101071001"
				},
				{
					"市名": "铁岭",
					"编码": "101071101"
				},
				{
					"市名": "朝阳",
					"编码": "101071201"
				},
				{
					"市名": "盘锦",
					"编码": "101071301"
				},
				{
					"市名": "大连",
					"编码": "101070201"
				},
				{
					"市名": "锦州",
					"编码": "101070701"
				}
			]
		}
	]
}

使用时候,可以把上面的数据保存为city_code.json文件导入程序代码中,然后以文件形式加载读取,如本例所示。

<think>好的,用户想要用Python编写一个国内旅游攻略自动化生成软件,集成DeepSeek API、和风天气API和高德地图API。首先,我需要理清楚用户的需求和各个API的功能。用户提到的功能流程图显示,用户输入城市名称和旅游天数后,系统需要获取景点和天气信息,智能拆分景点,关联附近美食,最后生成时间表。 首先,用户可能是一个开发者或者有一定编程基础的人,想要自动化生成旅游攻略,节省时间和精力。他们的真实需求不仅是整合多个API,还要确保整个流程顺畅,数据准确,并且生成的时间表合理实用。可能还有一些未明确的需求,比如错误处理、用户体验优化等。 接下来,我需要考虑各个API的使用方法。和风天气API用于获取天气数据,需要注册获取API key,并了解其返回的数据结构,比如是否有每日天气的daily字段,之前用户提到的KeyError可能就是因为数据结构不符,所以需要注意这点。高德地图API用于获取景点和附近美食,需要调用他们的POI搜索和周边推荐接口。DeepSeek API可能用于自然语言处理,生成旅游建议或时间表描述,但用户没有具体说明用途,可能需要进一步确认。 然后,设计整个系统的架构。用户输入城市和天数后,系统需要依次调用天气API获取天气情况,高德地图获取景点列表,然后根据天数智能拆分景点,可能要考虑景点之间的距离、开放时间、用户评分等因素。接着,为每个景点查找附近的美食,最后生成详细的时间安排表。 在智能拆分景点部分,可能需要算法来优化路线,比如基于地理位置聚类,或者使用旅行商问题(TSP)的近似解法,但考虑到时间限制,可能需要简化处理,比如按区域划分或者热门程度排序。关联附近美食需要调用高德地图的周边搜索接口,传入景点坐标和餐饮类别的关键词。 生成时间表时,需要合理分配每天的景点数量,考虑交通时间和用餐时间,可能需要预设每个景点的游览时长,并结合天气情况调整户外活动安排。 错误处理也很重要,比如API调用失败、数据解析错误、网络问题等,需要添加try-except块,友好的错误提示,并可能重试机制。同时,用户可能需要保存生成的攻略,或者导出为PDF、Excel格式,但用户没有提到,可能需要后续扩展。 代码结构方面,使用函数模块化每个步骤:获取天气、获取景点、拆分景点、查找美食、生成时间表。使用配置文件管理API密钥,避免硬编码。可能需要异步处理API请求以提高效率,但同步请求更简单,适合初步实现。 测试环节需要模拟不同城市的输入,检查各个API的响应是否正确,处理边界情况,比如旅游天数过多或过少,城市名称错误等情况。用户可能在实际使用中遇到各种问题,比如高德API的限流,需要处理HTTP状态码和响应数据中的错误码。 最后,用户可能需要一个简单的命令行界面或图形界面,但根据问题描述,可能只需要核心功能的实现。需要编写文档说明如何配置API密钥和运行程序。 总结下来,开发步骤包括:注册API获取密钥,编写各个API的调用函数,实现数据处理和逻辑整合,设计智能拆分算法,处理异常情况,测试并优化用户体验。需要确保代码的可维护性和扩展性,方便后续添加新功能或更换API服务商。</think>--- ### **国内旅游攻略自动化生成软件架构设计** 以下基于Python实现多API集成方案,使用模块化设计确保可扩展性。 --- ### **一、技术选型** | 组件 | 用途 | 官方文档 | |---------------|-------------------------------|-------------------------------------| | DeepSeek API | 生成自然语言描述的行程建议 | [DeepSeek开发者平台](https://platform.deepseek.com/) | | 和风天气API | 获取指定日期天气数据 | [和风天气开发文档](https://dev.qweather.com/) | | 高德地图API | 获取景点POI/周边美食/路线规划 | [高德开放平台](https://lbs.amap.com/) | --- ### **二、核心功能实现代码** #### **1. 基础配置** ```python import requests import json from datetime import datetime, timedelta # API配置 CONFIG = { "qweather": { "key": "YOUR_QWEATHER_KEY", "daily_url": "https://api.qweather.com/v7/weather/7d" }, "amap": { "key": "YOUR_AMAP_KEY", "poi_url": "https://restapi.amap.com/v3/place/text", "around_url": "https://restapi.amap.com/v3/place/around" }, "deepseek": { "key": "YOUR_DEEPSEEK_KEY", "api_url": "https://api.deepseek.com/v1/chat/completions" } } ``` #### **2. 天气获取模块** ```python def get_weather(city: str, date: str) -> dict: """ 获取指定城市日期的天气数据 返回格式:{"date": "2024-05-20", "temp": "20~28℃", "weather": "晴"} """ params = { "location": get_city_code(city), # 需实现城市名转adcode "key": CONFIG["qweather"]["key"] } try: response = requests.get(CONFIG["qweather"]["daily_url"], params=params) data = response.json() if "daily" not in data: raise ValueError("API返回数据异常") for day_data in data["daily"]: if day_data["fxDate"] == date: return { "date": date, "temp": f"{day_data['tempMin']}~{day_data['tempMax']}℃", "weather": day_data["textDay"] } return {"error": "超出预报范围"} except Exception as e: return {"error": str(e)} ``` #### **3. 景点与美食获取模块** ```python def get_attractions(city: str, max_num=20) -> list: """获取城市热门景点列表""" params = { "key": CONFIG["amap"]["key"], "keywords": "景点", "city": city, "types": "风景名胜", "output": "json" } response = requests.get(CONFIG["amap"]["poi_url"], params=params) return sorted( response.json()["pois"], key=lambda x: float(x["biz_ext"]["rating"] or 0), reverse=True )[:max_num] def find_nearby_food(location: str, radius=1000) -> list: """根据坐标查找周边美食(高德坐标系)""" params = { "key": CONFIG["amap"]["key"], "location": location, "radius": radius, "types": "餐饮", "sortrule": "distance" } response = requests.get(CONFIG["amap"]["around_url"], params=params) return response.json()["pois"] ``` #### **4. 智能行程生成模块** ```python def generate_itinerary(spots: list, days: int) -> list: """ 景点智能拆分算法 输入:排序后的景点列表、旅游天数 输出:按天分组的景点集合 """ # 基础版本:简单均分(后续可升级为基于距离聚类) avg = len(spots) // days return [spots[i*avg : (i+1)*avg] for i in range(days)] ``` #### **5. DeepSeek AI增强模块** ```python def generate_description(itinerary: list) -> str: """使用DeepSeek生成自然语言描述""" headers = { "Authorization": f"Bearer {CONFIG['deepseek']['key']}", "Content-Type": "application/json" } prompt = f"请将以下行程转化为旅游攻略:{json.dumps(itinerary)}" payload = { "model": "deepseek-chat", "messages": [{"role": "user", "content": prompt}] } response = requests.post(CONFIG["deepseek"]["api_url"], headers=headers, json=payload) return response.json()["choices"][0]["message"]["content"] ``` --- ### **三、主程序工作流** ```python def main(): # 用户输入 city = input("请输入旅游城市:") days = int(input("请输入旅游天数:")) start_date = input("请输入出发日期(YYYY-MM-DD):") # 获取基础数据 weather_data = [get_weather(city, (datetime.strptime(start_date, "%Y-%m-%d") + timedelta(days=i)).strftime("%Y-%m-%d")) for i in range(days)] spots = get_attractions(city) # 生成行程 daily_spots = generate_itinerary(spots, days) # 关联美食 full_itinerary = [] for day_idx, spots_group in enumerate(daily_spots): day_plan = { "date": weather_data[day_idx]["date"], "weather": weather_data[day_idx]["weather"], "spots": [] } for spot in spots_group: location = spot["location"].split(",") foods = find_nearby_food(f"{location[0]},{location[1]}") day_plan["spots"].append({ "name": spot["name"], "address": spot["address"], "recommend_foods": [food["name"] for food in foods[:3]] }) full_itinerary.append(day_plan) # AI生成描述 print(generate_description(full_itinerary)) if __name__ == "__main__": main() ``` --- ### **四、关键优化方向** 1. **路线智能优化** ```python # 使用高德路径规划API计算景点间距离 def get_route_time(origin: str, destination: str) -> float: params = { "key": CONFIG["amap"]["key"], "origin": origin, "destination": destination, "strategy": "2" # 最短距离策略 } response = requests.get("https://restapi.amap.com/v3/direction/driving", params=params) return float(response.json()["route"]["paths"][0]["duration"])/60 # 返回分钟数 ``` 2. **负载均衡设计** ```python # 动态调整每日景点数量 def dynamic_schedule(spots: list, days: int) -> list: # 根据景点评分+预计游览时间进行加权计算 weighted_spots = [] for spot in spots: time = 2.0 if "公园" in spot["type"] else 1.5 # 基础时间预估 weighted_spots.append( (spot, float(spot["biz_ext"]["rating"])*0.7 + time*0.3) ) # 使用背包算法进行分配 # ...(具体实现代码) ``` 3. **异常处理增强** ```python # 在API调用处添加重试机制 from tenacity import retry, stop_after_attempt, wait_fixed @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) def safe_api_call(url: str, params: dict): response = requests.get(url, params=params, timeout=5) response.raise_for_status() return response.json() ``` --- ### **五、部署与扩展建议** 1. **前端界面**:使用PyQt/Gradio快速搭建操作界面 2. **数据缓存**:对天气/景点数据建立本地缓存(SQLite) 3. **输出格式**:支持生成PDF/Excel文件 4. **实时更新**:添加微信/邮件通知功能 建议先从核心功能入手,逐步迭代优化。实际开发中需特别注意各API的调用频率限制,建议添加使用量监控模块。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangphil

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

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

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

打赏作者

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

抵扣说明:

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

余额充值