How to Get HTML Source from Url with Swift

How To Get HTML source from URL with Swift

Disclaimer : Since this is getting quite a lot of views, I just want to remind everyone that this answer here is synchronous, and will block your app if you do it on the main thread. You should always do this asynchronously (in a background thread), but the question asked for a synchronous method, so it would be out of scope to explain how to do it here.


You should probably look at the method :

+ stringWithContentsOfURL:encoding:error (docs)

You would call it like this in Objective C :

NSString *myURLString = @"http://google.com";
NSURL *myURL = [NSURL URLWithString:myURLString];

NSError *error = nil;
NSString *myHTMLString = [NSString stringWithContentsOfURL:myURL encoding: NSUTF8StringEncoding error:&error];

if (error != nil)
{
NSLog(@"Error : %@", error);
}
else
{
NSLog(@"HTML : %@", myHTMLString);
}

So in Swift 3 and 4, the equivalent would be :

let myURLString = "https://google.com"
guard let myURL = URL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}

do {
let myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
print("HTML : \(myHTMLString)")
} catch let error {
print("Error: \(error)")
}

You might want to adapt the encoding (see the constants) depending on which encoding your page's using.


Old answer, Swift 2.2 :

let myURLString = "http://google.com"
guard let myURL = NSURL(string: myURLString) else {
print("Error: \(myURLString) doesn't seem to be a valid URL")
return
}

do {
let myHTMLString = try String(contentsOfURL: myURL)
print("HTML : \(myHTMLString)")
} catch let error as NSError {
print("Error: \(error)")
}

Old answer, Swift 1.2 :

let myURLString = "http://google.com"

if let myURL = NSURL(string: myURLString) {
var error: NSError?
let myHTMLString = NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error)

if let error = error {
println("Error : \(error)")
} else {
println("HTML : \(myHTMLString)")
}
} else {
println("Error: \(myURLString) doesn't seem to be a valid URL")
}

How to get HTML Source from URL in Swift Anonymously?

It will always do this anonymously :

var request = URLRequest(url: URL(string: "http://google.com")!)
request.httpMethod = "GET"
let session = URLSession.init(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) {data,response,error in
if let data = data {
let contents = String(data: data, encoding: .ascii)
}
}.resume()

Get the HTML content when hitting URL in swift 3

Use the string initializer with the url.

do {
let contents = try String(contentsOf: URLstr, encoding: .ascii)
} catch {
// handle error
}

Or you can use URLSession.

let task = URLSession.shared.dataTask(with: URLStr) { data, response, error in
guard data != nil else { // no data }
let contents = String(data: data!, encoding: .ascii)
}
task.resume()

Swift get HTML from URL

It seems that www.google.com sends the response using the
ISO 8859-1 encoding, the corresponding NSString encoding is NSISOLatin1StringEncoding:

html = try NSString(contentsOfURL: testUrl!, encoding: NSISOLatin1StringEncoding)

You can also detect the HTTP response encoding automatically,
see for example https://stackoverflow.com/a/32051684/1187415.

Weird output from URL html source Swift

It's unclear why you want to encode the output to UTF-16, although removing it should work:

try! print(String(contentsOf: URL(string: "https://www.google.com")!))

That should return the html of the URL.

You can use an encoding, however, UTF-16 is likely not correct in this circumstance.

try! print(String(contentsOf: URL(string: "https://www.google.com")!, encoding: .ascii))

ASCII would likely work.

Get HTML from WKWebview in Swift

If you wait until the page has loaded you can use:

webView.evaluateJavaScript("document.documentElement.outerHTML.toString()", 
completionHandler: { (html: Any?, error: Error?) in
print(html)
})

You could also inject some javascript that returns you back the HTML.

let script = WKUserScript(source: javascriptString, injectionTime: injectionTime, forMainFrameOnly: true)
userContentController.addUserScript(script)
self.webView.configuration.userContentController.addScriptMessageHandler(self, name: "didGetHTML")



func userContentController(userContentController: WKUserContentController,
didReceiveScriptMessage message: WKScriptMessage) {

if message.name == "didGetHTML" {
if let html = message.body as? String {
print(html)
}
}
}

The javascript you could inject looks something like:

webkit.messageHandlers.didGetHTML.postMessage(document.documentElement.outerHTML.toString());

How Can I Retrieve the HTML Code for A Webpage In Swift

This should do the trick: (adapted from the objective-c in this answer)

let url = NSURL(string: "http://www.example.com")
var error: NSError?
let html = NSString(contentsOfURL: url!, encoding: NSUTF8StringEncoding, error: &error)

if (error != nil) {
println("whoops, something went wrong")
} else {
println(html!)
}


Related Topics



Leave a reply



Submit