Alamofire Swift获取HTML源码 [英] Alamofire Swift get html source
问题描述
我只想从一个简单的网站中检索html源。
I just want to retrieve the html source from a simple website.
@IBAction func scan_func(sender: AnyObject) {
Alamofire.request(.GET, "http://www.example.com")
.response { request, response, data, error in
print(request)
print(response)
print(data)
}
}
我已经成功地在info.plist中添加了应用程序传输安全设置和允许任意加载以加载http内容。
I already have successfully added "App Transport Security Settings" and "Allow Arbitrary Loads" to info.plist to load http content.
运行该代码时,我只会得到这样的输出:
XCODE-十六进制输出
我希望您可以帮助我。
When I run that code I only get an output like this: XCODE - hexadecimal output I hope you can help me.
亲切的问候
推荐答案
您正在打印取出数据的原始字节。我猜您正在寻找一种打印相应字符串表示形式的方法。
例如,您可以使用Alamofire提供的闭包来完成此操作:
You are printing out the raw bytes of the data. I'm guessing you are looking for a way to print out the corresponding string representation. You could for instance do this with Alamofire's provided closure:
Alamofire.request(.GET, "http://www.example.com")
.responseString { response in
print("Response String: \(response.result.value)")
}
(查看文档此处)
或者,您也可以自己转换字符串:
Alternatively, you could convert the string yourself:
Alamofire.request(.GET, "http://www.example.com")
.response { request, response, data, error in
print(String(data: data, encoding: String.Encoding.utf8))
}
< a href = https://developer.apple.com/reference/swift/string/1418413-init rel = noreferrer> Apple String参考文档
这篇关于Alamofire Swift获取HTML源码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!