1

I'm using Hpple to parse HTML of a website onto my app. The parsing is working great but instead of all the contents of the tr block to be in one cell, each of the td elements in the tr block are table cells of their own. Here's what I mean. The TR Block:

<tr>
                    <td>2</td>
                    <td>down 1</td>
                    <td>1</td>
                    <td>5</td>
                    <td>Justin Bieber</td>
                    <td>What Do You Mean?</td>
                </tr>

What it looks like in the app:
enter image description here

When I want it to actually look like this: enter image description here

The code I'm using for the parsing looks like this:

 - (void)loadSongs {
// 1
NSURL *tutorialsUrl = [NSURL URLWithString:@"http://www.bbc.co.uk/radio1/chart/singles/print"];
NSData *tutorialsHtmlData = [NSData dataWithContentsOfURL:tutorialsUrl];

// 2
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];

// 3
NSString *tutorialsXpathQueryString = @"//tr/td";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

// 4
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
    // 5
    Tutorial *tutorial = [[Tutorial alloc] init];
    [newTutorials addObject:tutorial];

    // 6
    tutorial.title = [[element firstChild] content];
   tutorial.peakPosition = ???;


}

// 8
_objects = newTutorials;
[self.tableView reloadData];
}
HalesEnchanted
  • 615
  • 2
  • 9
  • 20

1 Answers1

0

Try NSString *tutorialsXpathQueryString = @"//tr[contains(td, *)]";

See here for more functions: w3schools

This website for testing your xpath syntax

Hope it helps :)

Update:

The reason why you get nil is because you didn't get html data from your url instead of the XPath.

And the reason why you can't get the data is because APP Transport Security.

Please check this link: How do I load an HTTP URL with App Transport Security enabled in iOS 9?

Add the following to your plist file.

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

It should be working : )

Community
  • 1
  • 1
wei
  • 4,267
  • 2
  • 23
  • 18
  • @HalesEnchanted Hey try the method I updated. It should work. If you have questions, please keep it updated. – wei Oct 04 '15 at 15:52
  • Hey @wei, Thanks for sticking around. I already did have App Transport disabled :( Still nothing :( – HalesEnchanted Oct 04 '15 at 23:23