How To Get HTML source from URL with Swift

前端 未结 5 1102
盖世英雄少女心
盖世英雄少女心 2020-12-04 22:41

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?

5条回答
  •  旧巷少年郎
    2020-12-04 22:47

    Swift 3:

        if let url = URL(string: "https://www.google.com/trends/hottrends/atom/hourly") {
            do {
                let contents = try String(contentsOf: url)
                print(contents)
            } catch {
                // contents could not be loaded
            }
        } else {
            // the URL was bad!
        }
    

提交回复
热议问题