0

I want to extract: 51.39%, from this website www.bayyinah.org

You can see the text just over the donate button 51.39% of $6 Million

This is the code that i am using to TRY get that value.

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

    if error == nil {

        let urlContent = NSString(data: data!, encoding: NSASCIIStringEncoding) as NSString!

        do {
            // if encoding is omitted, it defaults to NSUTF8StringEncoding
            let doc = try HTMLDocument(string: urlContent as String, encoding: NSUTF8StringEncoding)

            // XPath queries
            for element in doc.xpath("//*[@id='so_far']/text()") {
                print(element)
            }

        } catch let error {
            print(error)
        }
    }
})
task.resume()

}

The result that i am getting is:

0%

Thank you!

0

1 Answer 1

1

You can't do what you are trying to achieve with XPath alone, the HTML itself contains the code:

<div class="row">
    <div class="mil"><strong id="so_far">0%</strong> <span class="light">of $6 Million</span></div>
</div>

You can see that if you run curl https://www.bayyinah.org/

It seems that the web-browser, after retrieving the HTML, subsequently executes some JavaScript which inserts the value.

However if you examine how they do this, you will see that they call this service https://www.bayyinah.org/api.php?action=get_so_far&clinet_tz=Europe%2FLondon which returns the following JSON with your answer:

{"total_so_far":51.498683333333}

So maybe just use their API?

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect love you man. Just a question. Where did you see that service they are using. when i press inspect i cant seem to find what you find
I used Chrome Developer tools to monitor the network requests they were making.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.