Why am I not getting any data after Parsing this HTML data using TFHpple

246 Views Asked by At

I am struggling to get any information back from my xPathQuery. The link I got using Chrome's developer tools, and did it by right clicking on the highlighted row in the screenshot attached. I'm creating a recipe-based app and am just playing around with single URL's before I try to scale it up. Help would be hugely appreciated!

override func viewDidLoad() {
    super.viewDidLoad()

    let url=NSURL(string:"http://www.bbc.co.uk/food/recipes/roast_shoulder_of_lamb_92545")

    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

        if error == nil {

            var parser = TFHpple(HTMLData: data)

            var xPString = "/html/body/table/tbody/tr[186]/td[2]/span[3]"

            var nodes = parser.searchWithXPathQuery(xPString) as? [TFHppleElement]

            println(nodes)

        }

    })

    task.resume()


}
1

There are 1 best solutions below

10
Dennis Weidmann On BEST ANSWER

Try to rename the following line from

var nodes = tutorialParser.searchWithXPathQuery(xPString) as? [TFHppleElement]

to

var nodes = parser.searchWithXPathQuery(xPString) as? [TFHppleElement]

Edit 1

I just took a look at the html code from the url you try to parse... I made a search with safaris developer tools for the substring "tbody"... it seems there is no "tbody" defined in the html, so your parser won't be able to find it and maybe will give you a empty array like he is doing it currently...

could you try to search without the "tbody" in your xPString?

So go from

var xPString = "/html/body/table/tbody/tr[186]/td[2]/span[3]"

to

var xPString = "/html/body/table/tr[186]/td[2]/span[3]"

also is there a 186th tr tag in the table? a 2nd td in this tr and 3 spans in this td?

Edit 2

Ok, here is your parser....

https://gist.github.com/DennisWeidmann/27ea76b5aed4718cf4ce

and here is a usage example

https://gist.github.com/DennisWeidmann/a66ce53ad8691744d6a6