码迷,mamicode.com
首页 > 其他好文 > 详细

获取城市代码

时间:2014-12-22 22:47:29      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

http://m.weather.com.cn/data5/city.xml

返回所有省/直辖市的编号

01|北京,02|上海,03|天津,04|重庆,05|黑龙江,06|吉林,07|辽宁,08|内蒙古,09|河北,10|山西,11|陕西,12|山东,13|新疆,14|西藏,15|青海,16|甘肃,17|宁夏,18|河南,19|江苏,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|贵州,27|四川,28|广东,29|云南,30|广西,31|海南,32|香港,33|澳门,34|台湾

http://m.weather.com.cn/data5/city21.xml

返回二级地区编号

2101|杭州,2102|湖州,2103|嘉兴,2104|宁波,2105|绍兴,2106|台州,2107|温州,2108|丽水,2109|金华,2110|衢州,2111|舟山

http://m.weather.com.cn/data5/city2106.xml

返回三级地区编号

210601|台州,210602|玉环,210603|三门,210604|天台,210605|仙居,210606|温岭,210607|洪家,210608|临海,210609|椒江,210610|黄岩,210611|路桥

获取所有地区编号的代码

#-*- coding:UTF-8 -*-
import urllib2

url1 = http://m.weather.com.cn/data5/city.xml
content1 = urllib2.urlopen(url1).read()
provinces = content1.split(,)
result = city = {\n
url = http://m.weather.com.cn/data3/city%s.xml
for p in provinces:
    p_code = p.split(|)[0]
    url2 = url % p_code
    content2 = urllib2.urlopen(url2).read()
    cities = content2.split(,)
    for c in cities:
        c_code = c.split(|)[0]
        url3 = url % c_code
        content3 = urllib2.urlopen(url3).read()
        districts = content3.split(,)
        for d in districts:
            d_pair = d.split(|)
            d_code = d_pair[0]
            name = d_pair[1]
            url4 = url % d_code
            content4 = urllib2.urlopen(url4).read()
            code = content4.split(|)[0]
            line = "    ‘%s‘: ‘%s‘,\n" % (name, code)
            result += line
            print  name + : + ‘101‘ + code
result += }
f = open(rE:\1.py, w)
f.write(result)
f.close()

打印结果可参考

https://gist.github.com/imbyron/6069825。

 

获取城市代码

标签:

原文地址:http://www.cnblogs.com/wangjibo/p/4179107.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!