I have next html structure
<div class="item column-1">
<h2>
<a href="/news/1172-zimnee-21-12-2014">
some text
</a>
</h2>
</div>
I want to get content of a tag, I'm using this code
NSURL *newURL = [NSURL URLWithString:@"http://mfc.mk.ua/news"];
NSData *newsData = [NSData dataWithContentsOfURL: newURL];
TFHpple *newsParser = [TFHpple hppleWithHTMLData: newsData];
NSString *newsXpathQueryString = @"//div[@class='item column-1']/h2/a";
NSArray *newsNodes = [newsParser searchWithXPathQuery: newsXpathQueryString];
NSMutableArray *newNews = [[NSMutableArray alloc] initWithCapacity: 0];
for (TFHppleElement *element in newsNodes)
{
News *news = [[News alloc] init];
[newNews addObject: news];
news.title = [[element firstChild] content];
_objects = newNews;
}
But it returning no results. If I do this:
news.title = [element objectForKey:@"href"];
then I'm getting results. What I doing wrong? Please help.
If you want "some text" in your example, rather than [[element firstChild] content]
, you should use simply [element content]
. If your example is representative, but you might want to trim white space and new lines, too. Thus:
news.title = [[element content] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];