我想从中国购物网站获取一些数据。但我无法找到一种编码我得到的数据的好方法。如果我使用NSNSUTF8StringEncoding,它会输出nil。如果使用NSASCIIStringEncoding,网站上的中文字符无法正确显示。有什么方法可以解决这个问题,还是应该使用像AFNetworking这样的第三个网络库?
以下是我的代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string:"https://item.taobao.com/item.htm?id=45457007854")!
let request = NSURLRequest(URL: url)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if let urlContent=data{
print(urlContent)
let webContent = NSString(data: urlContent, encoding:NSASCIIStringEncoding)
print(webContent)
}
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
部分结果如下所示
print(urlContent)
3a226b67 2f63756e 74616f2d 6379636c 652d6465 7461696c 2f302e30 2e322f22 2c227072 65636f6e 64697469 6f6e223a 22675f63 6f6e6669 672e6375 6e74616f 4379636c 65497465 6d222c22 696e6974 223a226e 65772043 6f6d706f 6e656e74 287b2474 61726765 743a2723 4a5f6375 6e74616f 4379636c 65277d29 222c226c 6f616422 3a22222c 22747269 67676572 223a2222 2c227265 74727922 3a317d29 3b0a7d29 28293b0a 0a3c2f73 63726970 743e3c73 63726970 74207372 633d222f 2f672e61 6c696364 6e2e636f 6d2f3f3f 6b697373 792f6b2f 312e342e 31342f73 6565642d 6d696e2e 6a732c74 622f676c 6f62616c 2f332e35 2e33352f 676c6f62 616c2d6d 696e2e6a 732c7462 2f697465 6d2d6465 7461696c 2f372e31 332e332f 706c6174 666f726d 2d6d696e 2e6a7322 20636861 72736574 3d227574 662d3822 3e3c2f73 63726970 743e0a20 2020200a 0a0a2020 20203c2f 626f6479 3e0a3c2f 68746d6c 3e0a>
print(webContent)
内容与您从Viewing Page source获得的内容几乎相同。
Optional(
<!doctype html>
<html><!-- cph -->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta charset="gbk"/>
<meta name="format-detection" content="telephone=no, address=no">
<link rel="dns-prefetch" href="//g.alicdn.com">
<link rel="dns-prefetch" href="//gtms01.alicdn.com">
<link rel="dns-prefetch" href="//gtms02.alicdn.com">
<link rel="dns-prefetch" href="//gtms03.alicdn.com">
<link rel="dns-prefetch" href="//gtms04.alicdn.com">
<link rel="dns-prefetch" href="//gd1.alicdn.com">
<link rel="dns-prefetch" href="//gd2.alicdn.com">
<link rel="dns-prefetch" href="//gd3.alicdn.com">
<link rel="dns-prefetch" href="//gd4.alicdn.com">
<link href="//item.taobao.com/item.html?id=45457007854" rel="canonical">
<link rel="alternate" hreflang="zh-Hant" href="http://tw.taobao.com/item/45457007854.htm" />
<meta name="renderer" content="webkit"/>
<meta name="referrer" content="always">
<meta name="description" content="»¶ÓÇ°À´ÌÔ±¦ÍøʵÁ¦ÍúÆÌ£¬Ñ¡¹ºÊÖ¶¯³È×ÓÕ¥ÖÆ÷Ò˼ÒÄûÃÊѹÖÆ÷¼·Ë®¹û¼ÐÔÖ»ú ³ø·¿ÓÃÆ·´´ÒâÉñÆ÷,ÏëÁ˽â¸ü¶àÊÖ¶¯³È×ÓÕ¥ÖÆ÷Ò˼ÒÄûÃÊѹÖÆ÷¼·Ë®¹û¼ÐÔÖ»ú ³ø·¿ÓÃÆ·´´ÒâÉñÆ÷£¬Çë½øÈë±Ë°¶Ê³Éеı˰¶Ê³ÉÐʵÁ¦ÍúÆÌ£¬¸ü¶àÉÌÆ·ÈÎÄãÑ¡¹º"/>
<meta name="keywords" content="ÌÔ±¦,Ìͱ¦,ÍøÉϹºÎï,µêÆÌ, ÊÖ¶¯³È×ÓÕ¥ÖÆ÷Ò˼ÒÄûÃÊѹÖÆ÷¼·Ë®¹û¼ÐÔÖ»ú ³ø·¿ÓÃÆ·´´ÒâÉñÆ÷."/>
<meta name="data-spm" content="2013"/>
<meta name="microscope-data"