整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

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. 运行效果

运行效果如下: