如何解析HTML以搜索特定于内容的针

时间:2014-03-18 01:57:25

标签: html ios objective-c xml parsing

鉴于下面的NSString最初是从使用CData解析XML文档时检索到的NSXMLParser对象转换而来的,如何获取该书的以下属性:title,book图片封面,作者,价格和评级?

这是我获得以下属性的基本解决方案

  1. 书名 - 我可以通过查看riRssTitle span类来获得这个,但后来我必须弄清楚如何读取ahref url之间的标题标记以获取标题

  2. 图书图片 - 我必须抓住第一个网址http://ecx.images-amazon.com/images/I/41Lg22K3ViL._SL160_PIsitb-sticker-arrow-dp,TopRight,12,-18_SH30_OU02_.jpg,然后将所有内容保留到http://ecx.images-amazon.com/images/I/41Lg22K3ViL,省略其余内容,然后附加.jpg标记,以便稍后获取完整的图像检索网址。

  3. 图书作者 - 我必须按照与步骤1相同的步骤,而是搜索riRssContributor span标记。

  4. 预订价格 - 这里没有价格标签在所有商品中都很常见,但我确实看到的一个共同点就是价格始终在font tag然后它位于BOLD tag

  5. 评分 - 可以通过查找包含单词stars的网址来检索,然后抓取其后面的数字,4表示4星星,附加-5的任何数字意味着额外的.5星。所以3-5意味着3.5星。

  6. 这样做的最好方法是什么,不要弄乱它?此外,我不喜欢我的代码如何破解,如果亚马逊决定改变它显示它的URL的方式,我的应用程序依赖亚马逊维护其URL命名约定。

    现在,这是前进的最佳方式吗?有没有一个可以实现我想做的快速解析器?

    这是亚马逊RSS提要的一个例子:http://www.amazon.co.uk/gp/rss/bestsellers/books/72/ref=zg_bs_72_rsslink


    以下是我为每件商品检索的以下CData NSString数据。

    <div style="float:left;">
        <a class="url" href="http://www.amazon.co.uk/Gone-Girl-Gillian-Flynn/dp/0753827662/ref=pd_zg_rss_ts_b_72_9">
            <img src="http://ecx.images-amazon.com/images/I/41Lg22K3ViL._SL160_PIsitb-sticker-arrow-dp,TopRight,12,-18_SH30_OU02_.jpg" alt="Gone Girl" border="0" hspace="0" vspace="0" />
        </a>
    </div>
    <span class="riRssTitle">
        <a href="http://www.amazon.co.uk/Gone-Girl-Gillian-Flynn/dp/0753827662/ref=pd_zg_rss_ts_b_72_9">Gone Girl</a>
    </span>
    <br />
    <span class="riRssContributor">
        <a href="http://www.amazon.co.uk/Gillian-Flynn/e/B001JP3W46/ref=ntt_athr_dp_pel_1">Gillian Flynn</a>
        <span class="byLinePipe">(Author)</span>
    </span>
    <br />
    <img src="http://g-ecx.images-amazon.com/images/G/02/x-locale/common/icons/uparrow_green_trans._V192561975_.gif" width="13" align="abstop" alt="Ranking has gone up in the past 24 hours" title="Ranking has gone up in the past 24 hours" height="11" border="0" />
    <font color="green">
        <strong></strong>
    </font> 674 days in the top 100 
    <br />
    <img src="http://g-ecx.images-amazon.com/images/G/02/detail/stars-4-0._V192253865_.gif" width="64" height="12" border="0" style="margin: 0; padding: 0;"/>(5704)
    <br />
    <br />
    <a href="http://www.amazon.co.uk/Gone-Girl-Gillian-Flynn/dp/0753827662/ref=pd_zg_rss_ts_b_72_9">Buy new: </a>
    <strike>£9.07</strike>
    <font color="#990000">
        <b>£3.85</b>
    </font>
    <br />
    <a href="http://www.amazon.co.uk/gp/offer-listing/0753827662/ref=pd_zg_rss_ts_b_72_9?ie=UTF8&condition=all">60 used & new</a> from 
    <span class="price">£2.21</span>
    <br />
    <br />(Visit the 
    <a href="http://www.amazon.co.uk/Best-Sellers-Books-Crime-Thrillers-Mystery/zgbs/books/72/ref=pd_zg_rss_ts_b_72_9">Bestsellers in Crime, Thrillers & Mystery</a> list for authoritative information on this product's current rank.)
    

4 个答案:

答案 0 :(得分:8)

TFHpple绝对是解析HTML的库。 (github上的&gt; 1000颗星) https://github.com/topfunky/hpple

以下是该RSS源的obj-c解决方案:

NSString *stringURL = @"http://www.amazon.co.uk/gp/rss/bestsellers/books/72/ref=zg_bs_72_rsslink";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *htmlData = [NSData dataWithContentsOfURL:url];

TFHpple * doc = [[TFHpple alloc] initWithHTMLData:htmlData];

NSArray *titleElements = [doc searchWithXPathQuery:@"//span[@class='riRssTitle']/a"];
for (TFHppleElement *element in titleElements)
{
    NSString *title = element.firstChild.content;
    NSLog(@"title: %@", title);
}

NSArray *imageElements = [doc searchWithXPathQuery:@"//a[@class='url']/img"];
for (TFHppleElement *element in imageElements)
{
    NSString *image = element.attributes[@"src"];
    NSMutableArray *parts = [[image componentsSeparatedByString:@"/"] mutableCopy];
    NSArray *pathParts = [parts.lastObject componentsSeparatedByString:@"."];
    [parts removeLastObject];
    [parts addObject:[NSString stringWithFormat:@"%@.%@",pathParts.firstObject, pathParts.lastObject]];
    image = [parts componentsJoinedByString:@"/"];
    NSLog(@"image: %@", image);
}

NSArray *authorElements = [doc searchWithXPathQuery:@"//span[@class='riRssContributor']/a"];
for (TFHppleElement *element in authorElements)
{
    NSString *author = element.firstChild.content;
    NSLog(@"author: %@", author);
}

NSArray *priceElements = [doc searchWithXPathQuery:@"//font/b"];
for (TFHppleElement *element in priceElements)
{
    NSString *price = element.firstChild.content;
    NSLog(@"price: %@", price);
}

NSArray *ratingElements = [doc searchWithXPathQuery:@"//img"];
for (TFHppleElement *element in ratingElements)
{
    if (![element.attributes[@"src"] containsString:@"stars"])
        continue;

    NSArray *parts = [element.attributes[@"src"] componentsSeparatedByString:@"-"];
    if (parts.count < 5) continue;

    NSString *rating = [NSString stringWithFormat:@"%@.%@", parts[3], [parts[4] substringToIndex:1]];
    NSLog(@"rating: %@", rating);
}

就像你说的那样,你受亚马逊命名惯例的支配。

答案 1 :(得分:4)

您可以使用TFHppleTFHppleElement根据需要解析上述数据。

Here是执行此操作的参考。

答案 2 :(得分:2)

我在iOS开发人员facebook小组上看到了你的帖子,并认为我会在最后一刻输入。

由于亚马逊没有严格的命名约定,因此您必须搜索Feed。 这就是我尝试做的事情,但后来我尝试让它看起来不那么骇人。 如果你注意到,你会发现,如果你试图搜索特定的路径名​​,有时候Feed会返回缺失值,所以我也试图弥补这种情况。

要实现此功能,您只需从此网址下载NSDictionary类别:https://github.com/nicklockwood/XMLDictionary

.h
#import <Foundation/Foundation.h>

@interface JMAmazonProcessor : NSObject
+(NSArray*)processAmazonResponseWithXMLData:(NSData*)responseObject;
@end

.m

#import "JMAmazonProcessor.h"

@implementation JMAmazonProcessor



+(NSString*)getBookTitleWithArray:(NSArray*)array{
    return [[array[0] objectForKey:kAmazonAHREFKey] objectForKey:kAmazonUnderscoreTextKey];
}

+(NSString*)getBookAuthorWithArray:(NSArray*)array{
    id bookAuthor = [[array[1] objectForKey:kAmazonAHREFKey] objectForKey:kAmazonUnderscoreTextKey];

    if(!bookAuthor){
        bookAuthor = [array[1] objectForKey:kAmazonUnderscoreTextKey];
    }

    if([bookAuthor isKindOfClass:[NSArray class]]){
        bookAuthor = [bookAuthor componentsJoinedByString:@" "];
    }

    return bookAuthor;
}
+(NSString*)getPriceFromDictionary:(NSDictionary*)dictionary{
    return [NSString stringWithUTF8String:[[[[dictionary objectForKey:@"font"] lastObject] objectForKey:@"b"] cStringUsingEncoding:NSUTF8StringEncoding]];
}


+(NSString*)getRatingWithCurrentRatingDictionary:(NSDictionary*)ratingDictionary{
    NSString * stars;
    if([ratingDictionary objectForKey:@"_src"]){
        NSString * possibleStarsURL = [ratingDictionary objectForKey:@"_src"];
        if([possibleStarsURL rangeOfString:@"stars-" options:NSCaseInsensitiveSearch].location != NSNotFound){
            stars = [[[[[possibleStarsURL componentsSeparatedByString:@"stars-"] lastObject] componentsSeparatedByString:@"."] firstObject] stringByReplacingOccurrencesOfString:@"-" withString:@"."];
        }
    }


    return stars;

}
+(NSString*)getRatingFromDictionary:(NSDictionary*)dictionary{
    id currentDictionary = [dictionary objectForKey:@"img"];
    NSString *rating;

    if([currentDictionary isKindOfClass:[NSArray class]]){
        for(int i = 0; i < [currentDictionary count]; i++){
            NSDictionary *currentRatingDictionary = [currentDictionary objectAtIndex:i];

            if((rating = [self getRatingWithCurrentRatingDictionary:currentRatingDictionary])){
                break;
            }
        }
    }

    else if([currentDictionary isKindOfClass:[NSDictionary class]]){
        rating = [self getRatingWithCurrentRatingDictionary:currentDictionary];
    }

    if(!rating) rating = @"Rating is not currently available";
    return rating;
}


+(NSArray*)processAmazonResponseWithXMLData:(NSData*)responseObject{
    NSMutableArray *bookEntries = [[NSMutableArray alloc] init];

    NSDictionary * itemDictionary = [[NSDictionary dictionaryWithXMLData:responseObject] objectForKey:kAmazonRootNode];
    for(int i = 0; i < [[itemDictionary objectForKey:kAmazonFeedItemKey] count]; i++){
        RSSBookEntryModel *cBEO = [[RSSBookEntryModel alloc] init];

        NSDictionary *currentItem = [[itemDictionary objectForKey:kAmazonFeedItemKey] objectAtIndex:i];
        NSString *finalXMLString = [NSString stringWithFormat:@"%@%@%@", kAmazonStartTag, [currentItem objectForKey:kAmazonDescriptionKey], kAmazonEndTag];
        NSDictionary *cData = [NSDictionary dictionaryWithXMLString:finalXMLString];

        NSArray *bookDetailsDictionary = [cData objectForKey:kAmazonSpanKey];

        NSString *bIOURL = [[[[cData objectForKey:@"div"] objectForKey:kAmazonAHREFKey] objectForKey:@"img"] objectForKey:@"_src"];
        NSString *bookImageCoverID = [[[[bIOURL componentsSeparatedByString:kAmazonBookCoverBaseURL] lastObject] componentsSeparatedByString:@"."] firstObject];




        cBEO.bookTitle = [self getBookTitleWithArray:bookDetailsDictionary];
        cBEO.bookAuthor = [self getBookAuthorWithArray:bookDetailsDictionary];
        cBEO.bookCoverImageThumbnailURL = [NSString stringWithFormat:@"%@%@%@%@", kAmazonBookCoverBaseURL, bookImageCoverID, kAmazonBookCoverThumbnailSize, kAmazonBookCoverFileExtention];
        cBEO.bookCoverImageOriginalURL = [NSString stringWithFormat:@"%@%@%@%@", kAmazonBookCoverBaseURL, bookImageCoverID, kAmazonBookCoverMaxSize, kAmazonBookCoverFileExtention];
        cBEO.bookPrice = [self getPriceFromDictionary:cData];
        cBEO.bookRating = [self getRatingFromDictionary:cData];

        [bookEntries addObject:cBEO];


    }
    return bookEntries;
}
@end

对不起。 这里是: 这是你想要使用的对象模型,非常简单。

@interface RSSBookEntryModel : NSObject

@property (strong, nonatomic) NSString *bookTitle;
@property (strong, nonatomic) NSString *bookAuthor;
@property (strong, nonatomic) NSString *bookCoverImageThumbnailURL;
@property (strong, nonatomic) NSString *bookCoverImageOriginalURL;
@property (strong, nonatomic) NSData *bookCoverThumbnailImage;
@property (strong, nonatomic) NSData *bookCoverOriginalImage;

@property (strong, nonatomic) NSString *bookPrice;
@property (strong, nonatomic) NSString *bookRating;


-(NSString*)description;

@end

以下是我用来保持一切清洁的常数。

Constant.h

extern NSString * const kAmazonRootNode;
extern NSString * const kAmazonStartTag;
extern NSString * const kAmazonEndTag;

extern NSString * const kAmazonFeedItemKey;
extern NSString *const kAmazonSpanKey;
extern NSString * const kAmazonDescriptionKey;
extern NSString *const kAmazonUnderscoreTextKey;
extern NSString *const kAmazonAHREFKey;
extern NSString *const kAmazonBookCoverBaseURL;

extern NSString *const kAmazonBookCoverThumbnailSize;
extern NSString *const kAmazonBookCoverMaxSize;
extern NSString *const kAmazonBookCoverFileExtention;

这是Constants.m文件。

NSString * const kAmazonRootNode = @"channel";
NSString * const kAmazonStartTag = @"<startTag>";
NSString * const kAmazonEndTag = @"</startTag>";

NSString * const kAmazonFeedItemKey = @"item";
NSString *const kAmazonSpanKey = @"span";
NSString * const kAmazonDescriptionKey = @"description";
NSString *const kAmazonUnderscoreTextKey = @"__text";
NSString *const kAmazonAHREFKey = @"a";
NSString *const kAmazonBookCoverBaseURL = @"http://ecx.images-amazon.com/images/";

NSString *const kAmazonBookCoverThumbnailSize = @"._SL100";
NSString *const kAmazonBookCoverMaxSize = @"._SL500";
NSString *const kAmazonBookCoverFileExtention = @".jpg";

答案 3 :(得分:1)

这是一个非常弱的选择,但也许它有所帮助:

//title
console.log("TITLE: " + $(".riRssTitle").text().trim());

//image
console.log("IMAGE: " + $(document).find("img").attr("src"));

//author
console.log("AUTHOR: " + $(".riRssContributor").find("a").text().trim());

//new price and striked price
var new_price_striked_element = $("a:contains('Buy new')").siblings("strike");
if(new_price_striked_element){
   console.log("NEW PRICE STRIKED: " + new_price_striked_element.text().trim());     
}else{
   console.log("NEW PRICE: " + $("a:contains('Buy new')").siblings("b").text().trim()); 
}

//used price
console.log("USED PRICE FROM: " + $(".price").text().trim());

//stars
var url = $("img[src*='stars']").attr("src");
var myRegexp = /stars-([0-9]-[0-9])/g;
var match = myRegexp.exec(url);
console.log("STARS: " + match[1]);

实施例 http://jsfiddle.net/qpuaxtv3/