iOS使用Hpple解析HTML属性 [英] iOS Parse HTML attribute with Hpple

查看:251
本文介绍了iOS使用Hpple解析HTML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在HTML的输入标签中检索属性.我的NSLog恢复为空.我的语法有什么问题?我正在尝试在输入标签中检索"value =的值.

I am trying to retrieve an attribute in an input tag in HTML. My NSLog is coming back null. What's wrong with my syntax? I am trying to retrieve the value for "value=" in the input tag.

这里是我正在解析的HTML:

Heres the HTML I am parsing:

<input name="hidXMLID" type="hidden" id="hidXMLID" value="0311sg55dbmnk0nsuzoflnij" />

使用解析器语法:

NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];
NSData *data = [[NSData alloc] initWithContentsOfURL:theURL];
xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];
TFHppleElement *element = [elements objectAtIndex:0];
NSString *content = [element content];

NSLog(@"ID = %@",content);

推荐答案

我想这可以通过使用不同的XPath查询和检查属性来更好地解决,例如:

I imagine this could be solved better with different XPath query and inspection of the attributes, something maybe like:

NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']"];
TFHppleElement *element = [elements objectAtIndex:0];
NSDictionary *attributes = [element attributes];
NSString *idValue = [attributes objectForKey:@"id"];

从您的帖子和日志中,我认为这可能在不更改查询的情况下起作用:

From your post and your logs, I think that this may work without changing the query:

TFHppleElement *element = [elements objectAtIndex:0];
TFHppleElement *child = [element.children objectAtIndex:0];
NSString *idValue = [child content];

这篇关于iOS使用Hpple解析HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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