Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

josh hahn
josh hahn
5,926 Points

Scrape web data with swift

I am trying to grab content from a webpage and everytime I load a webpage into a webview i get an error I have no idea what is going wrong this is the whole of my code

@IBOutlet weak var webView: WKWebView! let url = URL(string: "https://www.google.com")

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Insert code here to initialize your application
    let request = URLRequest(url: url!)
    webView.load(request)
 webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML", completionHandler: {(value, error) in
    if value != nil {print("INNER HTML: \(value)")}else{
        print("ERROR: \(error)")}
    })
}