如何从rss字符串中提取图像源 [英] How to extract the image source from an rss string

查看:108
本文介绍了如何从rss字符串中提取图像源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is the yahoo news rss feed
<p><a href="http://us.rd.yahoo.com/dailynews/rss/us/*http://news.yahoo.com/s/ap/20110521/ap_on_re_us/us_michelle_obama_west_point"><img src="http://d.yimg.com/a/p/ap/20110520/capt.53a8af4c675f421c8b7cba014c589d29-53a8af4c675f421c8b7cba014c589d29-0.jpg?x=130&y=97&q=85&sig=iOyBvkyg0rEKSK3hjlSsNA--" align="left" height="97" width="130" alt="First lady Michelle Obama, left, and Lt. Gen. David Huntoon, superintendent at the U.S. Military Academy, arrive in Washington Hall for a graduation banquet, Friday, May 20, 2011, in West Point, N.Y. (AP Photo/Mike Groll)" border="0" /></a>AP - First lady Michelle Obama urged more than 1,000 cadets Friday night on the brink of graduating to keep in mind the families of the soldiers they will lead.</p><br clear="all"/>

我如何提取图像源(http://d.yimg.com/a/p /ap/20110520/capt.53a8af4c675f421c8b7cba014c589d29-53a8af4c675f421c8b7cba014c589d29-0.jpg)来自此字符串。

how can i extract the image source (http://d.yimg.com/a/p/ap/20110520/capt.53a8af4c675f421c8b7cba014c589d29-53a8af4c675f421c8b7cba014c589d29-0.jpg) from this string.

推荐答案

您可以使用 NSScanner 。这样的事情:

You can use NSScanner. Something like this:

NSString *src = nil;
NSString *newsRSSFeed = @"<p><a href=\"http://us.rd.yahoo.com/dailynews/rss/us/*http://news.yahoo.com/s/ap/20110521/ap_on_re_us/us_michelle_obama_west_point\"><img src=\"http://d.yimg.com/a/p/ap/20110520/capt.53a8af4c675f421c8b7cba014c589d29-53a8af4c675f421c8b7cba014c589d29-0.jpg?x=130&y=97&q=85&sig=iOyBvkyg0rEKSK3hjlSsNA--\" align=\"left\" height=\"97\" width=\"130\" alt=\"First lady Michelle Obama, left, and Lt. Gen. David Huntoon, superintendent at the U.S. Military Academy, arrive in Washington Hall for a graduation banquet, Friday, May 20, 2011, in West Point, N.Y. (AP Photo/Mike Groll)\" border=\"0\" /></a>AP - First lady Michelle Obama urged more than 1,000 cadets Friday night on the brink of graduating to keep in mind the families of the soldiers they will lead.</p><br clear=\"all\"/>";
NSScanner *theScanner = [NSScanner scannerWithString:newsRSSFeed];
// find start of IMG tag
[theScanner scanUpToString:@"<img" intoString:nil];
if (![theScanner isAtEnd]) {
    [theScanner scanUpToString:@"src" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@"\"'"];
    [theScanner scanUpToCharactersFromSet:charset intoString:nil];
    [theScanner scanCharactersFromSet:charset intoString:nil];
    [theScanner scanUpToCharactersFromSet:charset intoString:&src];
    // src now contains the URL of the img
}
NSLog(@"%@",src);
// --> http://d.yimg.com/a/p/ap/20110520/capt.53a8af4c675f421c8b7cba014c589d29-53a8af4c675f421c8b7cba014c589d29-0.jpg?x=130&y=97&q=85&sig=iOyBvkyg0rEKSK3hjlSsNA--

这篇关于如何从rss字符串中提取图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆