设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Python 第46课】 查天气(4)

  [复制链接]

0

主题

0

好友

72

积分

注册会员

Rank: 2

楼主
发表于 2016-10-29 07:16:54 |显示全部楼层
Crossing 先生

python版本3.5,mac系统

#coding:utf-8
'''
Created on 2016年10月24日

@author: apple
'''
#!/usr/bin/env python
import urllib.request
url1 = 'http://m.weather.com.cn/data5/city.xml'
content1 = urllib.request.urlopen(url1).read()
print(content1)
provinces = content1.split(',')
print(provinces)

报错

Traceback (most recent call last):
  File "/Users/apple/Documents/workspace/testagain/src/weather.py", line 12, in <module>
b'01|\xe5\x8c\x97\xe4\xba\xac,02|\xe4\xb8\x8a\xe6\xb5\xb7,03|\xe5\xa4\xa9\xe6\xb4\xa5,04|\xe9\x87\x8d\xe5\xba\x86,05|\xe9\xbb\x91\xe9\xbe\x99\xe6\xb1\x9f,06|\xe5\x90\x89\xe6\x9e\x97,07|\xe8\xbe\xbd\xe5\xae\x81,08|\xe5\x86\x85\xe8\x92\x99\xe5\x8f\xa4,09|\xe6\xb2\xb3\xe5\x8c\x97,10|\xe5\xb1\xb1\xe8\xa5\xbf,11|\xe9\x99\x95\xe8\xa5\xbf,12|\xe5\xb1\xb1\xe4\xb8\x9c,13|\xe6\x96\xb0\xe7\x96\x86,14|\xe8\xa5\xbf\xe8\x97\x8f,15|\xe9\x9d\x92\xe6\xb5\xb7,16|\xe7\x94\x98\xe8\x82\x83,17|\xe5\xae\x81\xe5\xa4\x8f,18|\xe6\xb2\xb3\xe5\x8d\x97,19|\xe6\xb1\x9f\xe8\x8b\x8f,20|\xe6\xb9\x96\xe5\x8c\x97,21|\xe6\xb5\x99\xe6\xb1\x9f,22|\xe5\xae\x89\xe5\xbe\xbd,23|\xe7\xa6\x8f\xe5\xbb\xba,24|\xe6\xb1\x9f\xe8\xa5\xbf,25|\xe6\xb9\x96\xe5\x8d\x97,26|\xe8\xb4\xb5\xe5\xb7\x9e,27|\xe5\x9b\x9b\xe5\xb7\x9d,28|\xe5\xb9\xbf\xe4\xb8\x9c,29|\xe4\xba\x91\xe5\x8d\x97,30|\xe5\xb9\xbf\xe8\xa5\xbf,31|\xe6\xb5\xb7\xe5\x8d\x97,32|\xe9\xa6\x99\xe6\xb8\xaf,33|\xe6\xbe\xb3\xe9\x97\xa8,34|\xe5\x8f\xb0\xe6\xb9\xbe'
    provinces = content1.split(',')
TypeError: a bytes-like object is required, not 'str'

完全零基础,问题比较傻,烦请先生讲解一下,感谢
回复

使用道具 举报

0

主题

0

好友

72

积分

注册会员

Rank: 2

沙发
发表于 2016-10-29 23:38:27 |显示全部楼层
crossin先生 发表于 2016-10-29 17:07
python3这里读到了一个bytes类型,你要先转成str
content1 = content1.decode('utf8')
然后再来做 split

解决了,多谢先生!

请求到数据大概就是传说中新手的好运气吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-10 23:06 , Processed in 0.025568 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部