#!coding: utf-8 import requests from lxml import etree class GetWeather(object): def __init__(self, city): self.dict_code = {} self.city = city self.url = "http://www.weather.com.cn/weather/%s.shtml" self.wea_list = [] def set_code(self): with open(r"E:\weather\citycode.txt", "r") as f: for line in f.readlines(): if line != "\n": L = line.split("=") try: self.dict_code[L[1].strip().decode("gbk")] = L[0] except IndexError: import pdb;pdb.set_trace() def get_code(self, city): if city in self.dict_code: return self.dict_code[city] raise Exception(u"城市不存在") def parse_weather(self): #import pdb;pdb.set_trace() city_num = self.get_code(self.city) url = self.url % city_num resp = requests.get(url) resp.encoding = 'utf-8' root = etree.HTML(resp.text) els = root.xpath("//ul[@class='t clearfix']")[0] for el in els: tmp_dict = {} date = el.xpath("h1/text()")[0] wea = el.xpath("p[@class='wea']/text()")[0] try: high = el.xpath("p[@class='tem']/span/text()")[0] except IndexError: high = "" low = el.xpath("p[@class='tem']/i/text()")[0] win = el.xpath("p[@class='win']/i/text()")[0] tmp_dict = {"date": date, "wea": wea, "high": high, "low": low, "win": win} self.wea_list.append(tmp_dict) self.show(self.wea_list) def show(self, wea_list): print u"%s一周内的天气情况:" % self.city for d in wea_list: print u"日期: %s" % d["date"] print u"天气: %s" % d["wea"] if d["high"] != "": print u"温度: %s - %s" % (d["low"], d["high"]) else: print u"温度: %s" % d["low"] print u"风: %s" % d["win"] print "------------------------" if __name__ == "__main__": print u"*城市一周天气查询*\n" city = raw_input(u"请输出想要查询的城市: ") print "\n" city = city.decode("gbk") test = GetWeather(city) test.set_code() test.parse_weather()