开发者

xpath Table Contents into an array

开发者 https://www.devze.com 2023-03-25 12:25 出处:网络
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.

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, f开发者_StackOverflowor 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];

}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消