Loading...

python 错误问题解决

获取天气信息

#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方法

posted @ 2015-01-30 16:52  微笑阳光哈*_*  阅读(221)  评论(0编辑  收藏  举报