python 错误问题解决

时间:2015-01-30 19:01:45   收藏:0   阅读:166

获取天气信息

#encoding:UTF-8
import urllib.request
import re
def getHtml(url): 
    page=urllib.request.urlopen(url)
    html=page.read()
    
    page.close() 
    return html.decode(UTF-8)
def getWeather(html): 
    reg=<a href=(.*?)>*</a>
    weatherList=re.compile(reg).findall(html) 
    return weatherList 
weatherList=getWeather(getHtml(http://gd.weather.com.cn/)) 
for weather in weatherList: 
    print (weather) 

 

常见问题:

1. TypeError: can‘t use a string pattern on a bytes-like object

   解决: python3中urllib.read返回的是bytes对象,  不是string,得把它转换成string对象,用bytes.decode方法

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!