日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

编码 data:text/html;c,iOS 用TFHpple抓取GB-2312编码的html页面,页面返回编码错误

發(fā)布時間:2025/5/22 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编码 data:text/html;c,iOS 用TFHpple抓取GB-2312编码的html页面,页面返回编码错误 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

HTMLParser方法可以參考https://github.com/zootreeves/Objective-C-HMTL-Parser

TFHpple方法的使用可以看https://github.com/topfunky/hpple

TFHpple,因為它很簡單,也好用,但是它的功能不是很完完善。比如,不能獲取children node。它是用XPath來定位和解析html或者xml。

hpple解析html中可能存在的問題:http://www.ccbase.net/post/66.html

具體的方法可以參考:(英文網(wǎng)址介紹)http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone

(中文網(wǎng)址介紹)http://blog.csdn.net/xiaoxuan415315/article/details/7788955;還要代碼下載,不過里面有錯

Objective c代碼

NSData?*htmlData?=?[NSString?stringWithContentsOfFile:[NSURL?URLWithString:@"http://www.baidu.com"]?encoding:NSUTF8StringEncoding?error:nil];

會報:[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x6bcad20

Objective c代碼

NSData?*htmlData??=?[NSString?stringWithContentsOfURL:[NSURL?URLWithString:@"http://www.baidu.com"]?encoding:NSUTF8StringEncoding?error:nil];

匹配還是有問題

應該這個更合適點

Objective c代碼

NSData?*htmlData?=?[NSData?dataWithContentsOfURL:[NSURL?URLWithString:@"http://www.baidu.com"]];

下面講一下我自己實現(xiàn)的過程中的關鍵代碼:

解析非utf-8頁面的思路 :

把網(wǎng)絡流返回的NSDate的GB2312(假設是這個)轉(zhuǎn)換成TFHpple能正確解析的UTF-8編碼的NSData;

將其中一行的轉(zhuǎn)換成UTF-8形式的

可以采用如下兩種方案解決:

方法一:

Objective c代碼

//轉(zhuǎn)換成GBK編碼

NSStringEncoding?gbEncoding?=?CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

NSData?*htmlData?=?[NSData?dataWithContentsOfURL:[NSURL?URLWithString:@"可以是非utf-8的網(wǎng)頁"]];

NSString?*htmlStr?=?[[[NSString?alloc]?initWithData:htmlData?encoding:gbEncoding]?autorelease];

方法二:

Objective c代碼

NSData?*htmlData?=?[NSData?dataWithContentsOfURL:[NSURL?URLWithString:@"可以是非utf-8的網(wǎng)頁"]];

CFStringRef?bgCFStr?=?CFStringCreateWithBytes(NULL,?[htmlData?bytes],?[htmlData?length],?kCFStringEncodingGB_18030_2000,?false);

NSString?*gbHtmlStr?=?(NSString?*)bgCFStr;

方法一或者二選其一,然后加下面的代碼就可以實現(xiàn)簡單解析了

Objective c代碼

NSString?*utf8HtmlStr?=?[htmlStr?stringByReplacingOccurrencesOfString:@""

withString:@""];

NSData?*htmlDataUTF8?=?[utf8HtmlStr?dataUsingEncoding:NSUTF8StringEncoding];

TFHpple?*xpathParser?=?[[TFHpple?alloc]?initWithHTMLData:htmlDataUTF8];

NSArray?*elements??=?[xpathParser?search:@"//option"];

TFHppleElement?*element?=?[elements?objectAtIndex:0];

NSString?*h3Tag?=?[element?content];

NSLog(@"%@",h3Tag);

mLabel.text?=?h3Tag;

[xpathParser?release];

[htmlData?release];

很有用的網(wǎng)址:http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

碰到的問題:NSData --》NSString

NSData*htmlData = [htmlStrdataUsingEncoding:NSUTF8StringEncoding];

NSString?--》NSData

NSString*htmlStr = [[[NSStringalloc]initWithData:htmlDataencoding:NSUTF8StringEncoding]autorelease];

總結(jié)

以上是生活随笔為你收集整理的编码 data:text/html;c,iOS 用TFHpple抓取GB-2312编码的html页面,页面返回编码错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。