IOS 如何使用 nsscanner 类找到完整的 RSS 提要链接 [英] IOS How to find full rss feed link with nsscanner class
问题描述
我正在从基于 rss 提要的项目中获取数据.通过在 google 上搜索,我发现通常在 HTML 源中以这种格式找到 RSS 链接.
I am working on fetching data from rss feed based project.From searching on google i found that generally RSS link found in this format in source of HTML.
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://feeds.abcnews.com/abcnews/topstories" />
因此,我必须使用 nsscanner 类从 HTML 源中查找 RSS 提要的链接.但我不知道正确的模式,我必须设置 scanUpToString:
和 haracterSetWithCharactersInString:
等.所以,请帮助我如何找到 RSS 提要的完整链接.
so, I have to use nsscanner class to find the link of RSS feed from HTML source. but i don't know proper pattern and which i have to set scanUpToString:
and haracterSetWithCharactersInString:
or etc.
So, please help me how to i find the full link of RSS feed.
这是我的尝试:
- (void)viewDidLoad {
NSString *googleString = @"http://abcnews.go.com/";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL encoding:NSASCIIStringEncoding
error:&error];
NSLog(@"%@",[self yourStringArrayWithHTMLSourceString:googlePage]);//will return NSMutableArray
}
-(NSMutableArray *)yourStringArrayWithHTMLSourceString:(NSString *)html
{
NSString *from = @"<a href=\"";
NSString *to = @"</a>";
NSMutableArray *array = [[NSMutableArray alloc]init];
NSScanner* scanner = [NSScanner scannerWithString:html];
[scanner scanUpToString:@"<link" intoString:nil];
if (![scanner isAtEnd]) {
NSString *url = nil;
[scanner scanUpToString:@"RSS Feed" intoString:nil];
NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"/>"];
[scanner scanUpToCharactersFromSet:charset intoString:nil];
[scanner scanCharactersFromSet:charset intoString:nil];
[scanner scanUpToCharactersFromSet:charset intoString:&url];
NSLog(@"%@",url);
// "url" now contains the URL of the img
}
return array;
}
目前我只能找到此代码的链接.
currently i am able find only link with this code .
输出:
但完整链接是:-
http://feeds.abcnews.com/abcnews/topstories
推荐答案
使用link"而不是此参考文献中的a"标签.
Use "link" instead of "a" tags from this reference.
参考:ios中提取href url并丢弃其余锚标记的正则表达式
这篇关于IOS 如何使用 nsscanner 类找到完整的 RSS 提要链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!