Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

ios - How To Get HTML source from URL with Swift

I need to look at the HTML of a page given by a certain URL. If I have this, what is the most efficient and synchronous way to get the HTML source for that URL using Swift? I haven't been able to find a concise way online that returns it into a variable as opposed to printing it in a completionHandler.

I need to manipulate the source outside of whatever call uses the URL. How is this done in Swift?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...