Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

objective c - Unable to get data from a div tag using HTML parsing (hpple) in iPhone

I am trying to parse the below link using hpple:

http://www.decanter.com/news/wine-news/529748/mimimum-pricing-opponents-slam-cameron-speech

Code:

- (void)parseURL:(NSURL *)url {
    NSData *htmlData = [NSData dataWithContentsOfURL:url];    
    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
    NSArray *elements  = [xpathParser searchWithXPathQuery:@"<div class="body" id="article-529748-body">"];
    NSLog(@"elements %@",elements);
    TFHppleElement *element = [elements objectAtIndex:0];
    NSString *myTitle = [element content];
    [xpathParser release];
}

but it is crashing. Crash Report:

XPath error : Invalid expression
<div class="body" id="article-529748-body">
^
XPath error : Invalid expression
<div class="body" id="article-529748-body">
^

How to solve this issue? why my elements array is empty? Am I parsing in a wrong way? I want to get the information available in that div tag.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check that your elements array is not empty

- (void)parseURL:(NSURL *)url {
NSData *htmlData = [NSData dataWithContentsOfURL:url];    
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements  = [xpathParser searchWithXPathQuery:@"<div class="body" id="article-529748-body">"];
NSLog(@"elements %@",elements);
if([elements count]){
    TFHppleElement *element = [elements objectAtIndex:0];
}
NSString *myTitle = [element content];
[xpathParser release];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...