-1

All I do is simply:

let html = try String(contentsOf: hereMyUrl)

but the content of html is not the same as it displayed with a browser. Why? I cannot find some tags with specific attributes. I suppose it is loaded later with js. But how to accomplish that with Swift?

However when I try to it with WKWebView:

        let wk = WKWebView()
        let request = URLRequest(url: URL(string: hereMyUrl)!)
        wk.load(request)
        wk.evaluateJavaScript("document.documentElement.outerHTML.toString()") { html, error in
            print("++++++===")
            print(html)
            print("++++++===")
        }

I get nil there. Why?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 4
    You should never use `String(contentsOf: URL)` method to download data asynchronously – Leo Dabus Jan 20 '21 at 19:14
  • And your web view is a fake, as it is not in the view hierarchy and has no size. What is your actual goal here? It looks like what you _really_ want is to scrape a page. Use URLSession. – matt Jan 20 '21 at 20:00
  • All I need is to get content from the page. I search the content by tag. How do I use urlsession? – Bartłomiej Semańczyk Jan 20 '21 at 20:01

1 Answers1

2

You just see your html content with that method. Not Javascript or any other scripts which runs as a program on the html and can change the content during the life cycle of the web page.

Abbas Sabeti
  • 141
  • 2
  • 9
  • I am trying to do this with WKWebView but got nil. Why? – Bartłomiej Semańczyk Jan 20 '21 at 19:50
  • I wouldn't recommend to use WKWebView in this way. There are bunch of better ideas in getting content from WKWebView. See [this](https://stackoverflow.com/questions/34751860/get-html-from-wkwebview-in-swift) as an example. WKWebView has a lot of more control on the process of getting data and you can use it, instead of getting contents yourself. – Abbas Sabeti Jan 20 '21 at 20:01
  • above doesnt work from the question you shared. I did it as it is in my question. And it doesnt work... – Bartłomiej Semańczyk Jan 20 '21 at 21:10