1

I'm writing an app in Swift 3.0 and I'm trying to scrape data from the search results of a webpage. I perform the search by including the search query as a parameter in the url, but the html that's getting returned to me has no results. I believe this is because the ajax on the webpage has not finished and populated the html with the search results by the time the html is returned to my app.

Question

How do I wait for the search results to load before getting the html?

EDIT: URL: https://uscdirectory.usc.edu/web/directory/faculty-staff/#basic=a

This url performs a search on the USC directory for the character 'a'. The html in my browser on my MacBook includes these tags:

<tbody>(Search results are here)</tbody>

but in my app the html that is returned to me has nothing:

<tbody></tbody>

This is because the webpage initially has no search results, and then some time later the ajax finishes and the table body is populated. How do I use a URLSessionDataTask object in Swift to wait until the ajax finishes and ONLY THEN give me the html, such that I actually get the search results?

jeremyabannister
  • 3,796
  • 3
  • 16
  • 25

2 Answers2

1

The workaround solution that I devised is unpleasant but functional. I created a WKWebView off screen and loaded the webpage I was interested in. I used a Timer to wait some arbitrary number of seconds for the javascript on the page to finish and then I retrieved the html from the webview

jeremyabannister
  • 3,796
  • 3
  • 16
  • 25
0

You can use property ajaxComplete() to fix this.

Ex:

$(document).ajaxComplete(function(){
    //your code to render HTML
});

It will render your HTML only when ajax request is complete.

  • The webpage I'm querying isn't mine, so I can't change it. Any other work around? I'm looking for a solution on the Swift side, because I am writing an app in Swift to scrape data from this webpage and right now it's not working for this reason – jeremyabannister Feb 07 '17 at 05:04
  • He's looking for Swift code that will execute after the ajax is complete – Dan Beaulieu Feb 07 '17 at 12:49