def getHtml(self, url): req = urllib2.Request(url) res = urllib2.urlopen(req) html = res.read().decode("gb2312").encode("utf8") res.close()
return html
def getWeather(self, html): patterndate = re.compile(">(\d{4}-\d{2}-\d{2} .+)<") date = patterndate.findall(html)
patternweather = re.compile(" (.+)
") weather = patternweather.findall(html)
patterntemperature = re.compile("
([-]?\d{1,2}.+)
") temperature = patterntemperature.findall(html)
if len(date)== 0: print"Cannot get city's weather you input" return for i in range(len(date)): print"%s"% date[i],"\t%s"% weather[i],"\t%s"% temperature[i]