- 帖子
- 36
- 精华
- 0
- 积分
- 243
- 阅读权限
- 30
- 注册时间
- 2013-8-20
- 最后登录
- 2015-9-6
|
- # -*- 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('|')[1]
- line = " '%s': '%s',\n" % (name, code)
- result += line
- print name + ':' + code
- result += '}'
- f = file('./mycity.py', 'w')
- f.write(result)
- f.close()
复制代码- urllib2.URLError: <urlopen error [Errno 10061] >
- File "g:\mypython\weather\getxml.py", line 24, in <module>
- content4 = urllib2.urlopen(url4).read()
- File "G:\Python27\Lib\urllib2.py", line 126, in urlopen
- return _opener.open(url, data, timeout)
- File "G:\Python27\Lib\urllib2.py", line 394, in open
- response = self._open(req, data)
- File "G:\Python27\Lib\urllib2.py", line 412, in _open
- '_open', req)
- File "G:\Python27\Lib\urllib2.py", line 372, in _call_chain
- result = func(*args)
- File "G:\Python27\Lib\urllib2.py", line 1199, in http_open
- return self.do_open(httplib.HTTPConnection, req)
- File "G:\Python27\Lib\urllib2.py", line 1174, in do_open
- raise URLError(err)
复制代码 输出的是乱码(需要再另外编成gbk才行。):- 鍖椾含:101010100
- 娴锋穩:101010200
- 鏈濋槼:101010300
- 椤轰箟:101010400
复制代码 谢谢解答一下
|
|