我正在尝试在HTML中检索输入标记中的属性。我的NSLog回来了。我的语法有什么问题?我正在尝试在输入标记中检索“value =”的值。
继承我正在解析的HTML:
<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);
答案 0 :(得分:3)
我认为通过不同的XPath查询和属性检查可以更好地解决这个问题,例如:
NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']"];
TFHppleElement *element = [elements objectAtIndex:0];
NSDictionary *attributes = [element attributes];
NSString *idValue = [attributes objectForKey:@"id"];
从您的帖子和日志中,我认为这可以在不更改查询的情况下运行:
TFHppleElement *element = [elements objectAtIndex:0];
TFHppleElement *child = [element.children objectAtIndex:0];
NSString *idValue = [child content];