zoukankan      html  css  js  c++  java
  • 天气信息+迭代器

    从网址中抓取城市的天气信息,实现可迭代对象和迭代器对象,从而方便的进行一组城市的天气信息查询

     1 # encoding=utf-8
     2 import requests
     3 # *************************************************************************************
     4 # 实现从网站中抓取天气信息
     5 # def getWeather(city):
     6 #     r = requests.get(u'http://wthrcdn.etouch.cn/weather_mini?city=' + city)
     7 #     data = r.json()['data']['forecast'][0]
     8 #     return '%s:%s,%s' % (city,data['low'],data['high'])
     9 
    10  # print getWeather(u'北京')
    11  # print getWeather(u'沈阳')
    12 # ***************************************************************************************
    13 from collections import Iterable,Iterator
    14 
    15 class WeatherIterator(Iterator):
    16     def __init__(self,cities):
    17         self.cities = cities
    18         self.index = 0
    19     
    20     def getWeather(self,city):
    21         r = requests.get(u'http://wthrcdn.etouch.cn/weather_mini?city=' + city)
    22         data = r.json()['data']['forecast'][0]
    23         return '%s:%s,%s' % (city,data['low'],data['high'])
    24     
    25     def next(self):
    26         if self.index == len(self.cities):
    27             raise StopIteration
    28         city = self.cities[self.index]
    29         self.index += 1
    30         return self.getWeather(city)
    31         
    32 class WeatherIterable(Iterable):
    33     def __init__(self,cities):
    34         self.cities = cities
    35     
    36     def __iter__(self):
    37         return WeatherIterator(self.cities)
    38         
    39         
    40         
    41 for x in WeatherIterable([u'北京',u'上海',u'沈阳',u'郑州']):
    42         print x
  • 相关阅读:
    ZJOI 2019 划水记
    【博弈论】浅谈泛Nim游戏
    HZNU ACM一日游 2019.3.17 【2,4,6-三硝基甲苯(TNT)】
    BZOJ 1008 越狱 组合数学
    BZOJ 1036 树的统计Count 树链剖分模板题
    BZOJ 1012 最大数maxnumber 线段树
    BZOJ 1001 狼抓兔子 平面图的最小割
    SGU---107 水题
    欧拉回路模板
    hdu-3397 Sequence operation 线段树多种标记
  • 原文地址:https://www.cnblogs.com/banshaohuan/p/6919885.html
Copyright © 2011-2022 走看看