python一周天气预报

#!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()

 

转载于:https://www.cnblogs.com/lrheisoo/p/7441479.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值