整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          iOS:怎么開發一個天氣APP項目(一)獲取數據

          .網絡請求數據

          首先你得有個天氣的API,API最好要求返回的JSON數據比較規范便于數據轉模型,由于只是初學只是練手所以無需找那些付費的API。

          在這里我是使用百度的API:http://apistore.baidu.com/apiworks/servicedetail/112.html

          其他天氣API可以自行百度。

          找到API之后進行網絡請求并且解析從服務器返回的天氣數據。

          在iOS9之后NSURLConnection被蘋果棄用而推薦使用NSURLSession,所以我這里使用NSURLSession。

          -(void)requestCityWeather{
          
           NSString *cityName = @"城市名"; 
           NSString *cityID = @"城市ID"; 
           //這個是API地址 
           NSString *httpUrl = @"http://apis.baidu.com/apistore/weatherservice/recentweathers"; 
           NSString *utf8 = [cityName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 
           NSString *httpArg = [NSString stringWithFormat:@"cityname=%@&cityid=%@",utf8,cityID]; 
           [self request:httpUrl withHttpArg:httpArg];
          }
          //獲取數據
          -(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg { 
           NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg]; 
           NSURL *url = [NSURL URLWithString: urlStr]; 
          
           //1.創建一個request
           NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; 
           [request setHTTPMethod: @"GET"]; 
           [request addValue: @"您自己的apikey" forHTTPHeaderField: @"apikey"]; 
           
           //2.進行網絡請求 
           NSURLSession *session = [NSURLSession sharedSession]; 
           NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { //當請求完成后執行這個block 請求下來的數據會在一個NSData里 if (error) { NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code); } else { NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode]; NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"HttpResponseCode:%ld", responseCode); NSLog(@"HttpResponseBody %@",responseString); //把JSON數據轉成字典 NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSDictionary *dataDic = [dataDict objectForKey:@"retData"]; NSLog(@"dataDic - %@",self.weatherDic); } }]; [task resume]; //不能漏}

          這時候天氣數據就在一個字典中了,那么第一步也就完成了。

          2.字典轉模型

          這時候你的數據在一個字典里,這時候需要字典轉模型。

          若有不明白字典轉模型的可以看這篇文章:http://www.cnblogs.com/wendingding/p/3749530.html

          也可以自行百度。

          這個是百度API返回的實例

          從返回的JSON數據來看需要嵌套模型,過濾掉不需要的信息。

          創建如上類

          WeatherData.h

          WeatherData.m

          TodayData.h

          TodayData.m??

          #import "TodayData.h"
          
          @implementation TodayData
          
          -(instancetype)initWithDic:(NSDictionary *)dic{
           self = [super init]; 
           if (self) {
           NSString *str = [dic[@"curTemp"] substringWithRange:NSMakeRange(0, [dic[@"curTemp"] length] - 1)]; 
           str = [str stringByAppendingString:@"°"]; 
           self.curTemp = str; 
           self.date = dic[@"date"]; 
           self.fengli = dic[@"fengli"]; 
           self.fengxiang = dic[@"fengxiang"]; 
           self.highTemp = dic[@"hightemp"]; 
           self.lowTemp = dic[@"lowtemp"]; 
           self.pm = dic[@"aqi"]; 
           self.week = dic[@"week"]; 
           self.type = dic[@"type"]; 
           self.time = dic[@"time"]; 
           self.api = [self getAirPollutionIndex]; 
           self.index = [TodayIndex initWithDic:dic[@"index"]]; 
           } 
           return self;
          }
          
          +(instancetype)initWithDic:(NSDictionary *)dic{
           return [[self alloc]initWithDic:dic];
          }
          
          -(NSString *)getAirPollutionIndex{
           NSLog(@"api: - %@",self.pm); 
           if ((NSNull *)self.pm == [NSNull null]){
           return @"無數據"; 
           }else if (self.pm.intValue <= 50) {
           return @"優"; 
           }else if (self.pm.intValue <= 100 && self.pm.intValue > 50){
           return @"良"; 
           }else if (self.pm.intValue <= 150 && self.pm.intValue > 100){
           return @"輕度污染"; 
           }else if (self.pm.intValue <= 200 && self.pm.intValue > 150){
           return @"中度污染"; 
           }else if (self.pm.intValue <= 300 && self.pm.intValue > 200){
           return @"重度污染"; 
           }else{
           return @"嚴重污染"; 
           }
          }
          @end

          其他模型類都是差不多的。

          寫好模型數據之后 數據部分就完成了。

          文/SoolyChristina(簡書作者)

          章目錄

          • 一、實現效果1. python代碼2. 運行效果二、基本思路1. 爬蟲部分2. tkinter界面

          私信小編01即可獲取大量Python學習資料

          一、實現效果

          1. python代碼

          import requests
          from lxml import etree
          import re
          import tkinter as tk
          from PIL import Image, ImageTk
          from xpinyin import Pinyin
          
          
          def get_image(file_nam, width, height):
              im = Image.open(file_nam).resize((width, height))
              return ImageTk.PhotoImage(im)
          
          
          def spider():
              headers = {
                  'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24',
                  "referer": "https://lishi.tianqi.com/chengdu/index.html"
              }
              p = Pinyin()
              place = ''.join(p.get_pinyin(b1.get()).split('-'))           # 獲取地區文本框的輸入  變為拼音
              # 處理用戶輸入的時間
              # 規定三種格式都可以 2018/10/1  2018年10月1日  2018-10-1
              date = b2.get()   # 獲取時間文本框的輸入
              if '/' in date:
                  tm_list = date.split('/')
              elif '-' in date:
                  tm_list = date.split('-')
              else:
                  tm_list = re.findall(r'\d+', date)
          
              if int(tm_list[1]) < 10:       # 1-9月  前面加 0
                  tm_list[1] = f'0{tm_list[1]}'
              # 分析網頁規律  構造url
              # 直接訪問有該月所有天氣信息的頁面 提高查詢效率
              url = f"https://lishi.tianqi.com/{place}/{''.join(tm_list[:2])}.html"
              resp = requests.get(url, headers=headers)
              html = etree.HTML(resp.text)
              # xpath定位提取該日天氣信息
              info = html.xpath(f'//ul[@class="thrui"]/li[{int(tm_list[2])}]/div/text()')
              # 輸出信息格式化一下
              info1 = ['日期:', '最高氣溫:', '最低氣溫:', '天氣:', '風向:']
              datas = [i + j for i, j in zip(info1, info)]
              info = '\n'.join(datas)
              t.insert('insert', '        查詢結果如下        \n\n')
              t.insert('insert', info)
              print(info)
          
          
          win = tk.Tk()
          win.title('全國各地歷史天氣查詢系統')
          win.geometry('500x500')
          
          # 畫布  設置背景圖片
          canvas = tk.Canvas(win, height=500, width=500)
          im_root = get_image('test.jpg', width=500, height=500)
          canvas.create_image(250, 250, image=im_root)
          canvas.pack()
          
          # 單行文本
          L1 = tk.Label(win, bg='yellow', text="地區:", font=("SimHei", 12))
          L2 = tk.Label(win, bg='yellow', text="時間:", font=("SimHei", 12))
          L1.place(x=85, y=100)
          L2.place(x=85, y=150)
          
          # 單行文本框  可采集鍵盤輸入
          b1 = tk.Entry(win, font=("SimHei", 12), show=None, width=35)
          b2 = tk.Entry(win, font=("SimHei", 12), show=None, width=35)
          b1.place(x=140, y=100)
          b2.place(x=140, y=150)
          
          # 設置查詢按鈕
          a = tk.Button(win, bg='red', text="查詢", width=25, height=2, command=spider)
          a.place(x=160, y=200)
          
          # 設置多行文本框  寬 高  文本框中字體  選中文字時文字的顏色
          t = tk.Text(win, width=30, height=8, font=("SimHei", 18), selectforeground='red')  # 顯示多行文本
          t.place(x=70, y=280)
          
          # 進入消息循環
          win.mainloop()
          

          2. 運行效果

          運行效果如下:

          ?


          主站蜘蛛池模板: 亚洲人成网站18禁止一区 | 久久精品日韩一区国产二区| 国产在线精品一区二区| 极品少妇伦理一区二区| 精品少妇ay一区二区三区 | 在线观看日本亚洲一区| 立川理惠在线播放一区| 亚洲一区二区观看播放| 亚洲国产精品乱码一区二区| 岛国精品一区免费视频在线观看| 无码aⅴ精品一区二区三区| 免费av一区二区三区| 国产伦精品一区二区三区视频金莲 | 国产美女av在线一区| 一区二区精品视频| 国产精品电影一区二区三区 | 精品无码人妻一区二区三区不卡| 亚洲AV无码一区二区三区在线| 肥臀熟女一区二区三区| 精品人体无码一区二区三区| 一区二区在线播放视频| 日韩精品无码久久一区二区三| 国产精品99精品一区二区三区 | 亚洲国产成人久久一区久久 | 亚洲国产一区二区三区青草影视| 综合人妻久久一区二区精品| 中文字幕一区二区三区免费视频| 无码精品蜜桃一区二区三区WW| 中文字幕在线播放一区| 精品一区二区三区高清免费观看 | 国产大秀视频一区二区三区| 波多野结衣一区二区三区高清av| 国产精品一区电影| 日韩熟女精品一区二区三区 | 国产aⅴ精品一区二区三区久久| 日本一区二区三区免费高清在线| 99久久精品国产免看国产一区| 色偷偷一区二区无码视频| 国产精品揄拍一区二区| 国产精品乱码一区二区三| 无码欧精品亚洲日韩一区夜夜嗨|