iOS에서 hpple을 사용하여 많은 하위 태그가있는 복잡한 html을 구문 분석하는 방법. 이 라이브러리에 대한 훌륭하고 복잡한 튜토리얼이나 좋은 문서를 찾을 수 없습니다. 간단한 구문 분석으로 문제가 없습니다. html 예는 다음과 같습니다.
<div class="post shortcuts_item focus" id="post_243273">
<div class="published">today in 21:03</div>
<h1 class="title">
<a href="http://example.ru/post/243273/" class="post_title">Some simple title text here</a>
</h1>
<div class="hubs">
<a href="http://example.ru/hub/internet_regulation/" class="hub">Simple text 1</a><span class="profiled_hub" title="Profile 1">*</span>,
<a href="http://example.ru/hub/business-laws/" class="hub">Simple text 2</a><span class="profiled_hub" title="Profile 2">*</span>,
<a href="http://example.ru/hub/vkontakte/" class="hub">Simple text 3</a><span class="profiled_hub" title="Profile 3">*</span>,
<a href="http://example.ru/hub/social_networks/" class="hub">Simple text 4</a><span class="profiled_hub" title="Profile 4">*</span>,
<a href="http://example.ru/hub/facebook/" class="hub">Simple text 5</a><span class="profiled_hub" title="Profile 5">*</span>
</div>
<div class="content html_format">
<img src="//example.org/files/aa9/f6c/8a0/aa9f6c8a049e405c9f72bf10ee3a2e9f.png"><br>
<br>
Simple but very long text with link tag <a href="http://example.ru/post/243273/#blahblah">Simple link...</a>
<div class="buttons">
<a href="http://example.ru/post/243273/#habracut">Read more...</a>
</div>
<div class="clear"></div>
</div>
</div>
문제는 한 번의 실행으로 모든 A
태그를 내부 에 가져 오는 방법 class="hubs"
입니다. 어떻게 할 수 있습니까?
//
검색 구문을 사용하여 HTML의 모든 위치를 검색 할 수 있지만 @class='xxx'
구문을 사용하여 특정 클래스의 태그를 검색 할 수 있습니다. 따라서 //a[@class='hub']
또는 //div[@class='hubs']/a
다음 중 하나를 검색 할 수 있습니다 .
TFHpple *parser = [TFHpple hppleWithHTMLData:data];
NSArray *nodes = [parser searchWithXPathQuery:@"//a[@class='hub']"];
for (TFHppleElement *element in nodes) {
NSString *href = [element attributes][@"href"];
NSString *content = [element content];
NSLog(@"%@ -> %@", href, content);
}
Ray Wenderlich 사이트 에서 iOS 에서 HTML을 구문 분석하는 방법을 참조하십시오 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다