HTML資料解析

2022-08-22 15:36:13 字數 2962 閱讀 9338

html資料解析 用到開源** htmlparser  :htmlnode.m  htmlnode.h  htmlparser.m htmlparser.h

解析你的資料前還有三步:

1在工程中新增libxml2的庫

2: 在header search path中新增/usr/include/libxml2

3: 將開源**加入到工程中去。並在引入標頭檔案

這樣我們就能開始解析 html的資料了

nsurl *url=[nsurl urlwithstring:@"

"]; nsstring *htmlstr=[[nsstring alloc]initwithcontentsofurl:url encoding:nsutf8stringencoding error:nil];

nserror *error;

//解析html文件

//建立乙個 htmlparser的物件

htmlparser *parser = [[htmlparser alloc] initwithstring:str error:&error];

if(error)

上面就將html資料 放到乙個 htmlparser的物件裡面去了。

這裡展示這個html資料的內容(這裡進行一定的刪減 對我們完全沒用的去掉了)

"

-//w3c//dtd xhtml 1.0 transitional//en""

">

"">id="

west

">

id="

middle2

">class="

lot">

class="

left

"id="

weiboimg

">

" border="

0" align="

left

" />

巨蟹座06/22-07/22

class="

clear

">

class="

tab">愛情運勢

class="

clear

">

class="

tab">健康指數

79%class="

tab">商談指數

84%class="

clear

">

class="

tab">幸運顏色

白色class="

tab">幸運數字

2class="

clear

">

class="

tab">速配星座

獅子座class="

tab"> 

class="

clear

">

class="

clear

">

class="

lotconts

">外型上的艷麗讓內在也增添**魅力,除此之外還有才智一起的相乘作用,不僅美麗,還會美得很有智慧型喲。

而今天對女性而言將你的感性運用在創作上將可獲得不錯的評價喔,像是手工藝等需要巧手與心思細密的興趣

嗜好有好作品可期的預感呢。

class="

clear

">

現在我們就可以用htmlparse裡的方法的進行一步一步的解析了

//

獲取html的body部分

htmlnode *node =[parser body];

//在node的基礎上,找到並

取得總結的那段話

htmlnode *sum=[node findchildofclass:@"

lotconts

"];//這個方法就是查詢乙個屬性,名字叫"lotconts"

的節點

//在node的基礎上,找到並取

獲取有效日期

htmlnode *effectdate=[node findchildofclass :@"

datea"];

//在node的基礎上,找到並取

獲取星座名字

htmlnode *name=[node findchildtag:@"

span

"];//這個方法就是查詢乙個標籤叫"

span

"的節點

//在node的基礎上,找到並取

獲取星座時間段

htmlnode *time=[name findchildtag:@"em"

];//

htmlnode *image=[node findchildtag:@"

img"

];nsstring *pic=[image getattributenamed:@"

src"

];nsurl *url1=[nsurl urlwithstring:pic];

// 得到節點的內容

[name contents];//返回的就是乙個字串 在這裡內容就是:巨蟹座

//同理

[sum contents];//內容就是:外型上的艷麗讓內在也增添**魅力,除此之外還有才智一起的相乘作用,不僅美麗,還會美得很有智慧型喲。

而今天對女性而言將你的感性運用在創作上將可獲得不錯的評價喔,像是手工藝等需要巧手與心思細密的興趣

嗜好有好作品可期的預感呢。

html資料的解析就是根據:標籤(tag),屬性(attribute)來利用htmlparser裡的方法,找到我們需要的子節點。

要記得我們最後找到的都是子節點,我們要取得內容還是要-(nsstring*)contents;方法來取得。

ios 解析Html資料

前段時間遇到後台返回資料,需要解析html格式資料,經過折騰了一會就直接寫了乙個方法呼叫 其實很簡單 我直接貼上 下方再附上一張 的截圖 直接看的 更容易一點 nsmutablearray analysishtml nsstring html else if imghtml rangeofstrin...

使用HTMLPaser解析HTML資料

1,首先我們通過 ns data data ns url 得到目標得html檔案如下 id tab id tab id tab2 divno attr class cls red hello id abc id id abc class blue id class style a1 style a1...

使用HtmlParser解析HTML

如果要對html進行解析,提取html的資料或者修改html資料,htmlparser是乙個不錯的選擇.使用htmlparser可以解析本地和網路上的html資料 parser parser new parser new winista.text.htmlparser.http.httpprotoc...