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];

posted @ 2014-05-27 12:02  mfrbuaa  阅读(250)  评论(0编辑  收藏  举报