zoukankan      html  css  js  c++  java
  • iOS开发之html解析

    使用XPath解析html

    可以从此处https://github.com/topfunky/hpple下载工程,将TFHpple.h,TFHpple.m,TFHppleElement.h,TFHppleElement.m,XPathQuery.h,XPathQuery.m加到自己的项目中,在Frameworks中导入libxml2.x
    iOS开发之html解析

     
    在项目中找到Other Linker Flags,加入-libxml2
    iOS开发之html解析

    在项目中找到Header Search Paths,加入/usr/include/libxml2
    代码如下:

    NSString *urlString = nil;

    urlString = @"http://www.xiyou.edu.cn/new/lm.jsp?urltype=tree.TreeTempUrl&wbtreeid=724";

    NSData *htmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];

    NSData *toHtmlData = [self toUTF8:htmlData];

    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:toHtmlData];

    NSArray *aArray = [xpathParser searchWithXPathQuery:@"//a"];

    if ([span count] > 0) {

                

                for (int i = 87; i < 102; i++) {

                                //从<a>的第82个开始取值,共获取15个值

                    TFHppleElement *aElement = [aArray objectAtIndex:i];    

                    NSArray *aArr = [aElement children];

                    TFHppleElement *aEle = [aArr objectAtIndex:0];

                    NSArray *aChild = [aEle children];

                    TFHppleElement *aChildEle = [aChild objectAtIndex:0];

                    NSArray *aChildren = [aChildEle children];

                    NSString *aStr = [[aChildren objectAtIndex:0] content];

                    NSLog(@"aStr:%@",aStr);

                    NSDictionary *aAttributeDict = [aElement attributes];

                    NSLog(@"aAttributeDict:%@",aAttributeDict);

                    

                                //获取a中的属性值

                    NSString *hrefStr = [NSString stringWithFormat:@"http://www.xiyou.edu.cn%@",[aAttributeDictobjectForKey:@"href"]];

                    NSLog(@"hrefStr:%@",hrefStr);

                    

                    [currentNewsArr addObject:aStr];

                    [currentHrefArr addObject:hrefStr];

                    

                }

    [htmlData release];

    [xpathParser release];

    }

    //如果解析的网页不是utf8编码,如gbk编码,可以先将其转换为utf8编码再对其进行解析

    -(NSData *) toUTF8:(NSData *)sourceData {  

        CFStringRef gbkStr = CFStringCreateWithBytes(NULL, [sourceData bytes], [sourceData length],kCFStringEncodingGB_18030_2000, false);  

        

        if (gbkStr == NULL) {  

            return nil;  

        } else {  

            NSString *gbkString = (NSString *)gbkStr; 

            //根据网页源代码中编码方式进行修改,此处为从gbk转换为utf8

                 NSString *utf8_String = [gbkString stringByReplacingOccurrencesOfString:@"META http-equiv="Content-Type" content="text/html; charset=GBK""  

                                                                          withString:@"META http-equiv="Content-Type" content="text/html; charset=UTF-8""];  

            

            return [utf8_String dataUsingEncoding:NSUTF8StringEncoding];                             

        }                                     

    }

  • 相关阅读:
    WAS日常维护中的重启时机——总结
    利用Shell生成Zabbix监控的数字报表
    Zabbix version upgrade (1.8.3 to 1.8.12)
    xeyes命令
    centos系统调节屏幕亮度
    centos7 安装kchmviewer 软件
    ftp使用FileZilla工具传输文件
    搭建vsftpd服务并实现本地用户访问
    centos中创建服务和关闭防火墙的基本命令
    阿里云vsftpd登录失败:530 Permission Denied.
  • 原文地址:https://www.cnblogs.com/fakemessi/p/4900901.html
Copyright © 2011-2022 走看看