无法在 iPhone 中使用 HTML 解析 (hpple) 从 div 标签获取数据
我正在尝试使用 hpple 解析以下链接:
http://www.decanter.com/news/wine-news/529748/mimimum-pricing-opponents-slam-cameron-speech
代码:
- (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];
}
但它正在崩溃。崩溃报告:
XPath error : Invalid expression
<div class="body" id="article-529748-body">
^
XPath error : Invalid expression
<div class="body" id="article-529748-body">
^
如何解决这个问题?为什么我的元素数组是空的?我是否以错误的方式解析?我想获取该 div 标签中的可用信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查您的 elements 数组不为空
Check that your elements array is not empty
尝试将其更改
为:
Try changing this:
To:
写这篇文章(两年后!)以防它对遇到类似问题的其他人有用。
为了解析 div 中的 html,您需要
因此,您可能需要使用递归来遍历 div 的节点树。
代码(ARC):
使用这种递归方法:
这给出了输出:
附言。今天下午阅读了上述 Wenderlich 教程;我相信更有经验的人可能会想出更优雅的解决方案!
Writing this (2 years later!) in case it's useful to someone else with a similar problem.
In order to parse the html within the div, you need to
Because of this you may need to use recursion to walk though the div's node-tree.
Code (ARC):
using this recursive method:
This gives the output:
PS. Only started playing with Hpple this p.m. after reading the aforementioned Wenderlich tutorial; I'm sure someone more experienced may come up with a more elegant solution!