ios项目开发(天气预报项目):使用正则获取 weather.com.cn网站信息

NSString *pattern = @"(?<=<td class=\"bigblod\">).*?(?=</td>)";
2.
3.    NSString *pattern1 = @"<img src=\"http://www.weather.com.cn/m2/i/icon_weather/50x36/.*?gif\">";
4.
5.    NSURL *URL = [NSURL URLWithString:@"http://www.weather.com.cn/weather/101010100.shtml"];
6.    NSString *string = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:nil];
7.
8.    NSRange range = NSMakeRange(0, string.length);
9.
10.    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionAnchorsMatchLines|NSRegularExpressionDotMatchesLineSeparators error:nil];
11.
12.    NSArray *matches = [regex matchesInString:string options:0 range:range];
13.
14.    NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options:NSRegularExpressionAnchorsMatchLines|NSRegularExpressionDotMatchesLineSeparators error:nil];
15.
16.    NSArray *matches1 = [regex1 matchesInString:string options:0 range:range];
17.
18.
19.    NSMutableString *s = [[NSMutableString alloc]init];
20.    [s appendString: @"<meta charset=\"utf-8\">"];
21.
22.    int i = 0;
23.    for (NSTextCheckingResult *match in matches) {
24.        NSTextCheckingResult *match1 = [matches1 objectAtIndex:i];
25.        i ++;
26.
27.        NSRange matchRange = [match range];
28.        NSRange matchRange1 = [match1 range];
29.        NSLog(@"Reg:%@",[string substringWithRange:matchRange]);
30.
31.        [s appendString:[string substringWithRange:matchRange1]];
32.        [s appendString:@"</br>"];
33.
34.        [s appendString:[string substringWithRange:matchRange]];
35.        [s appendString:@"</br>"];
36.
37.
38.    }
39.
40.    [self.webview1 loadHTMLString:s baseURL:nil];

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值