iOS html解析1(包含中文字符)

本文探讨了Objective-C环境下HTML解析方法的选择与应用,包括如何利用TFHpple进行基本解析,并解决了非UTF-8编码网页的转换问题。通过详细步骤展示了将GB2312编码网页转换为UTF-8编码,进而实现有效解析的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

TFHpple,因为它很简单,也好用,但是它的功能不是很完完善。比如,不能获取children node。它是用XPath来定位和解析html或者xml。


xpath教程:http://www.w3school.com.cn/xpath/index.asp

具体的方法可以参考:(英文网址介绍)http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone

(中文网址介绍)http://blog.youkuaiyun.com/xiaoxuan415315/article/details/7788955;还要代码下载,不过里面有错

Objective c代码 收藏代码
  1. <span>NSData*htmlData=[NSStringstringWithContentsOfFile:[NSURLURLWithString:@"http://www.baidu.com</span>"]encoding:NSUTF8StringEncodingerror:nil];

会报:[NSURL getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x6bcad20

Objective c代码 收藏代码
  1. NSData*htmlData=[NSStringstringWithContentsOfURL:[NSURLURLWithString:@"http://www.baidu.com"]encoding:NSUTF8StringEncodingerror:nil];

匹配还是有问题

应该这个更合适点

Objective c代码 收藏代码
  1. <span>NSData*htmlData=[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.baidu.com
  2. </span>"]];

下面讲一下我自己实现的过程中的关键代码:

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

  1. 把网络流返回的NSDate的GB2312(假设是这个)转换成TFHpple能正确解析的UTF-8编码的NSData;
  2. 将其中一行的<meta http-equiv="Content-Type" content="text/html; charset=gb2312">转换成UTF-8形式的

可以采用如下两种方案解决:

方法一:

Objective c代码 收藏代码
  1. //转换成GBK编码
  2. NSStringEncodinggbEncoding=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
  3. NSData*htmlData=[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"可以是非utf-8的网页"]];
  4. NSString*htmlStr=[[[NSStringalloc]initWithData:htmlDataencoding:gbEncoding]autorelease];

方法二:

Objective c代码 收藏代码
  1. NSData*htmlData=[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"可以是非utf-8的网页"]];
  2. CFStringRefbgCFStr=CFStringCreateWithBytes(NULL,[htmlDatabytes],[htmlDatalength],kCFStringEncodingGB_18030_2000,false);
  3. NSString*gbHtmlStr=(NSString*)bgCFStr;

方法一或者二选其一,然后加下面的代码就可以实现简单解析了

Objective c代码 收藏代码
  1. NSString*utf8HtmlStr=[htmlStrstringByReplacingOccurrencesOfString:@"<metahttp-equiv=\"Content-Type\"content=\"text/html;charset=gb2312\">"
  2. withString:@"<metahttp-equiv=\"Content-Type\"content=\"text/html;charset=utf-8\">"];
  3. NSData*htmlDataUTF8=[utf8HtmlStrdataUsingEncoding:NSUTF8StringEncoding];
  4. TFHpple*xpathParser=[[TFHpplealloc]initWithHTMLData:htmlDataUTF8];
  5. NSArray*elements=[xpathParsersearch:@"//option"];
  6. TFHppleElement*element=[elementsobjectAtIndex:0];
  7. NSString*h3Tag=[elementcontent];
  8. NSLog(@"%@",h3Tag);
  9. mLabel.text=h3Tag;
  10. [xpathParserrelease];
  11. [htmlDatarelease];

很有用的网址: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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值