xpath 表内容放入数组中
我将如何在 hpple Objective-C 包装器下使用 xPath 来从该表中提取元素。理想情况下,我想将地址和价格放入数组或字典中。
我一直在尝试使用 hpple Objective C 包装器从此处的表中提取数据: http://www.gaspry.com/index.php?zipcode=02169&action=search 但尚未成功获得正确的 xpath 查询来提取信息。理想情况下,我想将这些信息存储在一组数组中,一个用于地址,一个用于每个价格。我被困在如何从表中检索所有信息,并且不确定我的 xPath 是否正确
我的代码到目前为止是:
- (void)viewDidLoad {
[super viewDidLoad];
//Url Information
NSString *urlAddress = @"http://www.gaspry.com/index.php?zipcode=02169&action=search";
NSURL *url = [NSURL URLWithString:urlAddress];
NSData *htmlData = [[NSData alloc] initWithContentsOfURL:url];
//Start Parsing
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser search:@"//tBody[tr]"]; // get the table information
TFHppleElement *element = [elements objectAtIndex:0];
NSString *bodyTag = [element content];
NSLog(@"%@", bodyTag);
[xpathParser release];
[htmlData release];
}
How would I use xPath under the hpple Objective-C wrapper to extract the elements out of this table. Ideally I would like to put the addresses and prices into an array or dictionary.
I have been attempting to use the hpple objective c wrapper to pull the data out of the table located here: http://www.gaspry.com/index.php?zipcode=02169&action=search but haven't succeeded in getting the correct xpath query to pull out the information. Ideally, I would like to store this information in a set of arrays one, for addresses, and one for each of the prices. I am stuck on how to retrieve all of the information from the table and am unsure that my xPath is correct
My Code Thus Far is:
- (void)viewDidLoad {
[super viewDidLoad];
//Url Information
NSString *urlAddress = @"http://www.gaspry.com/index.php?zipcode=02169&action=search";
NSURL *url = [NSURL URLWithString:urlAddress];
NSData *htmlData = [[NSData alloc] initWithContentsOfURL:url];
//Start Parsing
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser search:@"//tBody[tr]"]; // get the table information
TFHppleElement *element = [elements objectAtIndex:0];
NSString *bodyTag = [element content];
NSLog(@"%@", bodyTag);
[xpathParser release];
[htmlData release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个建议是,搜索路径区分大小写,因此请确保使用正确的路径。
搜索查询:
@"//tbody//tr[1]//td[1]"
将获取您的地址并更改列“td[#]”以访问您可能想要的其他内容店铺。
我不确定,但地址不会被正确解析,也就是说,上面的搜索查询将在
标签,但不会为您提供
标签之前的内容。
希望这有帮助:D
One advice, a search path is case sensitive so make sure you use right one.
search query for :
@"//tbody//tr[1]//td[1]"
will get you address and change column "td[#]" to access other things you may want to store.
I'm not sure but address won't be parse correctly, that is, above search query will result after
tag but won't give you content before
tag.
Hope this help :D