iOS a 标签链接:<a href ="http://www.baidu.com">xxxx</a>提取xxxx的3种方法

来源:互联网 发布:资生堂化妆水知乎 编辑:程序博客网 时间:2024/04/19 12:35
突发奇想,想做个分析新浪微博粉丝的应用。要提取出来新浪微博的小尾巴。 


//请求获取最新的一条微博
- (void)timelineButtonPressed
{
    SinaWeibo *sinaweibo = [self sinaweibo];
//    [sinaweibo requestWithURL:@"statuses/user_timeline.json"
//                       params:[NSMutableDictionary dictionaryWithObject:sinaweibo.userID forKey:@"uid"]
//                   httpMethod:@"GET"
//                     delegate:self];
    [sinaweibo requestWithURL:@"statuses/home_timeline.json"
                       params:nil
                   httpMethod:@"GET"
                     delegate:self];
    
    
}

- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result
{
//最新一条微博
     if ([request.url hasSuffix:@"statuses/home_timeline.json"])
    {
        [statuses release];
        
//请求成功,返回一个json,提取出来 statuses字段放进一个数组里
        statuses = [[result objectForKey:@"statuses"] retain];
        NSDictionary *dic=[statuses objectAtIndex:0];
        //source关键字对应的value 就是微博的来源,如<a href="http://app.weibo.com/t/feed/3ciyef" rel="nofollow">脉搏网</a> 前面地址是微博应用的地址,脉搏网就是我们要提取出来的尾巴
        NSString *string = [dic objectForKey:@"source"];
//用>分割字符串,放进数组里,取出第二个元素,截取字符串 就可得到脉搏网
        NSArray *array=[string componentsSeparatedByString:@">"];
        NSString *separateString=[array objectAtIndex:1];
        NSLog(@"%@",[separateString substringToIndex:(separateString.length-3)]);

//用来获取json statuses字段里的 所有关键字key 并打印出来key 和对应的value 从中发现source字段使我们所需要的
//        for (int i=0; i<dic.allKeys.count; i++)
//        {
//            NSLog(@"key==%@",[dic.allKeys objectAtIndex:i]);
//            NSLog(@"value==%@",[dic objectForKey:[dic.allKeys objectAtIndex:i]]);
//        }
        
      }
}
 
   <a href="http://app.weibo.com/t/feed/3ciyef" rel="nofollow">脉搏网</a> ,取出脉搏网有三种方法:
1、正则表达式
<a .*>(\d+|\D+)</a> 
2、字符串处理,提取><之间的值
3、分割字符串,就是上面例子里的写法。
阅读全文
0 0
原创粉丝点击