- 帖子
- 5
- 精华
- 0
- 积分
- 16
- 阅读权限
- 10
- 注册时间
- 2018-3-11
- 最后登录
- 2018-3-19
|
感觉py3在学的时候老是会一些意外的问题...运行之后会显示错误:
provinces = content1.split(',')
TypeError: a bytes-like object is required, not 'str'
请问我应该在哪里改正呢?- # -*- coding: gbk -*-
- import urllib.request
- import json
- url1 = 'http://m.weather.com.cn/data3/city.xml'
- content1 = urllib.request.urlopen(url1).read()
- #content1 = content1.decode('utf8')
- #print(content1)
- provinces = content1.split(',')
- #print(provinces)
- 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 = urllib.request.urlopen(url2).read()
- cities = content2.split(',')
- for c in cities:
- c_code = c.split('|')[0]
- url3 = url % c_code
- content3 = urllib.request.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 = urllib.urlopen(url4).read()
- code = content4.split('|')[1]
- line = " '%s': '%s',\n" % (name, code)
- result += line
- print(name + ':' + code)
- result += '}'
- f = open('city.py','w')
- f.write(result)
- f.close()
复制代码 |
|