- 帖子
- 6
- 精华
- 0
- 积分
- 28
- 阅读权限
- 10
- 注册时间
- 2017-12-4
- 最后登录
- 2017-12-12
|
import urllib.request
areacode={}
f=open('areacode.py','a+')
f.write('areacode={')
url = ('http://m.weather.com.cn/data3/city.xml')
content = urllib.request.urlopen(url).read()
content=content.decode('utf-8')
provinces=content.split(',')
url1 = 'http://m.weather.com.cn/data3/city%s.xml'
for p in provinces[:3]:
p_code=p.split('|')[0]
url2=url1 % p_code
content = urllib.request.urlopen(url2).read()
content = content.decode('utf-8')
city=content.split(',')
for c in city:
c_code = c.split('|')[0]
url3 = url1 % c_code
content = urllib.request.urlopen(url3).read()
content = content.decode('utf-8')
area=content.split(',')
for a in area:
date = a.split('|')
code=date[0]
name=date[1]
areacode[name] = code
print(name)
f.write('%s:%s,\n'%(name,code))
f.write('}')
f.close()
以上是获取地区编号的代码,用的windows系统,加了编码转换,打印出来是汉子,运行没报错,但是储存结果的areacode.py全是乱码
而且之前的查天气部分也不能识别
|
|