2012-09-18 5 views
0

웹 페이지에서 특정 div의 컨텐츠를 가져 오기 위해 hipple 라이브러리를 사용하고 있습니다.특정 div의 xpath 찾기

사용법 : 이제

NSData* data = [trimmedString dataUsingEncoding:NSUTF8StringEncoding]; 

TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
NSArray *elements = [xpathParser searchWithXPathQuery:@"//div[@id='container']"]; 
TFHppleElement *element = [elements objectAtIndex:0]; 
NSString *string = [element content]; 
NSLog(string); 

내가 그 사업부 컨테이너의 내용에 문자열 * 문자열을 작성해야 생각 해요?

는이 오류와 충돌을 받고 있어요 :

* 인해 캐치되지 않는 예외 'NSRangeException'응용 프로그램 종료, 이유는 : '* - [__ NSArrayM objectAtIndex :] : 빈 배열 범위를 넘는 인덱스 0 '

이제는 해당 div의 배열이 채워지지 않았기 때문입니다. 그 이유 중 하나는 그것이 내가 준 링크를 파싱하지 못했지만 xpath 파서에 추락하지 않았기 때문에 그 파스텔을 잘 추측 할 수 있을까요? 잘못이 줄을 나에게 그 내 XPath를 알려줍니다

:

searchWithXPathQuery:@"//div[@id='content']"]; 

사람이 나 텍스트 "나는 아래의 모든 텍스트가 필요합니다 :"아래의 DIV에 대한 XPath를 찾을 수 있습니다 (내가 제공 한 위의 모든 div의는

<body class="html not-front not-logged-in no-sidebars page-node page-node- page-node-68 node-type-article" > 
<div id="container"> 
    <section id="main" role="main"> 
     <header> 
      <div id="logo"><a href="/"><img src="/sites/all/themes/bmco/images/logo.gif" /></a></div> 
      <nav class="navigation" id="mainmenu"> 
        <div class="region region-menupanel"> 
    <div id="block-superfish-1" class="block block-superfish"> 


    <div class="content"> 
    <ul id="superfish-1" class="sf-menu main-menu sf-horizontal sf-style-default sf-total-items-7 sf-parent-items-5 sf-single-items-2"><li id="menu-566-1" class="first odd sf-item-1 sf-depth-1 sf-no-children"><a href="/" title="" class="sf-depth-1 ">home</a></li><li id="menu-567-1" class="middle even sf-item-2 sf-depth-1 sf-total-children-6 sf-parent-children-0 sf-single-children-6 menuparent"><a href="/company" title="data centre design and build" class="sf-depth-1 menuparent">company</a><ul><li id="menu-395-1" class="first odd sf-item-1 sf-depth-2 sf-no-children"><a href="/case-studies" class="sf-depth-2 ">case studies</a></li><li id="menu-393-1" class="middle even sf-item-2 sf-depth-2 sf-no-children"><a href="/news" class="sf-depth-2 ">news</a></li><li id="menu-608-1" class="middle odd sf-item-3 sf-depth-2 sf-no-children"><a href="/downloads" title="" class="sf-depth-2 ">downloads</a></li><li id="menu-572-1" class="middle even sf-item-4 sf-depth-2 sf-no-children"><a href="/meet-team" class="sf-depth-2 ">meet the team</a></li><li id="menu-609-1" class="middle odd sf-item-5 sf-depth-2 sf-no-children"><a href="/working-you" title="2bm is committed to being a responsible business. We aim to ensure that, in everything we do, we meet the highest standards of business ethics and conduct." class="sf-depth-2 ">working with you</a></li><li id="menu-607-1" class="last even sf-item-6 sf-depth-2 sf-no-children"><a href="/blog" title="" class="sf-depth-2 ">blog</a></li></ul></li><li id="menu-573-1" class="middle odd sf-item-3 sf-depth-1 sf-total-children-12 sf-parent-children-0 sf-single-children-12 menuparent"><a href="/design-and-build" title="" class="sf-depth-1 menuparent">design and build</a><ul><li id="menu-583-1" class="first odd sf-item-1 sf-depth-2 sf-no-children"><a href="/design-and-build/access-and-security" class="sf-depth-2 ">access and security</a></li><li id="menu-577-1" class="middle even sf-item-2 sf-depth-2 sf-no-children"><a href="/design-and-build/cabling" class="sf-depth-2 ">cabling</a></li><li id="menu-585-1" class="middle odd sf-item-3 sf-depth-2 sf-no-children"><a href="/design-and-build/clinical-cleaning" class="sf-depth-2 ">clinical cleaning</a></li><li id="menu-580-1" class="middle even sf-item-4 sf-depth-2 sf-no-children"><a href="/design-and-build/cooling" class="sf-depth-2 ">cooling</a></li><li id="menu-579-1" class="middle odd sf-item-5 sf-depth-2 sf-no-children"><a href="/design-and-build/electrical-supply" class="sf-depth-2 ">electrical supply</a></li><li id="menu-582-1" class="middle even sf-item-6 sf-depth-2 sf-no-children"><a href="/design-and-build/fire-supression" title="Data Centre Fire Suppression and Detection" class="sf-depth-2 ">fire suppression</a></li><li id="menu-574-1" class="middle odd sf-item-7 sf-depth-2 sf-no-children"><a href="/design-and-build/flooring" class="sf-depth-2 ">flooring</a></li><li id="menu-586-1" class="middle even sf-item-8 sf-depth-2 sf-no-children"><a href="/design-and-build/maintenance" class="sf-depth-2 ">maintenance</a></li><li id="menu-581-1" class="middle odd sf-item-9 sf-depth-2 sf-no-children"><a href="/design-and-build/power-monitoring" class="sf-depth-2 ">power monitoring</a></li><li id="menu-578-1" class="middle even sf-item-10 sf-depth-2 sf-no-children"><a href="/design-and-build/project-management" class="sf-depth-2 ">project management</a></li><li id="menu-584-1" class="middle odd sf-item-11 sf-depth-2 sf-no-children"><a href="/design-and-build/racking" class="sf-depth-2 ">racking</a></li><li id="menu-575-1" class="last even sf-item-12 sf-depth-2 sf-no-children"><a href="/design-and-build/room-construction-and-layout" class="sf-depth-2 ">room construction and layout</a></li></ul></li><li id="menu-588-1" class="middle even sf-item-4 sf-depth-1 sf-total-children-3 sf-parent-children-0 sf-single-children-3 menuparent"><a href="/monitoring" title="" class="sf-depth-1 menuparent">monitoring</a><ul><li id="menu-590-1" class="first odd sf-item-1 sf-depth-2 sf-no-children"><a href="/monitoring/environmental-monitoring" class="sf-depth-2 ">environmental monitoring</a></li><li id="menu-592-1" class="middle even sf-item-2 sf-depth-2 sf-no-children"><a href="/monitoring/innovative-cabling" class="sf-depth-2 ">innovative cabling</a></li><li id="menu-589-1" class="last odd sf-item-3 sf-depth-2 sf-no-children"><a href="/monitoring/sensorium-software" class="sf-depth-2 ">sensorium software</a></li></ul></li><li id="menu-587-1" class="middle odd sf-item-5 sf-depth-1 sf-total-children-10 sf-parent-children-0 sf-single-children-10 menuparent"><a href="/room-and-rack-infrastructure" title="" class="sf-depth-1 menuparent">room and rack</a><ul><li id="menu-598-1" class="first odd sf-item-1 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/iec-lock" class="sf-depth-2 ">IEC lock</a></li><li id="menu-594-1" class="middle even sf-item-2 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/kvm" class="sf-depth-2 ">KVM</a></li><li id="menu-597-1" class="middle odd sf-item-3 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/ups" class="sf-depth-2 ">UPS</a></li><li id="menu-576-1" class="middle even sf-item-4 sf-depth-2 sf-no-children"><a href="/design-and-build/airflow-management" class="sf-depth-2 ">airflow management</a></li><li id="menu-595-1" class="middle odd sf-item-5 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/aisle-containment" class="sf-depth-2 ">aisle containment</a></li><li id="menu-599-1" class="middle even sf-item-6 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/clinical-cleaning" class="sf-depth-2 ">clinical cleaning</a></li><li id="menu-593-1" class="middle odd sf-item-7 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/floor-tiles" class="sf-depth-2 ">floor tiles</a></li><li id="menu-596-1" class="middle even sf-item-8 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/power-strips" class="sf-depth-2 ">power strips</a></li><li id="menu-600-1" class="middle odd sf-item-9 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/racks" class="sf-depth-2 ">racks</a></li><li id="menu-601-1" class="last even sf-item-10 sf-depth-2 sf-no-children"><a href="/room-and-rack-infrastructure/security" class="sf-depth-2 ">security</a></li></ul></li><li id="menu-591-1" class="middle even sf-item-6 sf-depth-1 sf-total-children-5 sf-parent-children-0 sf-single-children-5 menuparent"><a href="/imeter" title="The 2bm iMeter - data centre power monitoring" class="sf-depth-1 menuparent">iMeter</a><ul><li id="menu-604-1" class="first odd sf-item-1 sf-depth-2 sf-no-children"><a href="/imeter/zero-downtime-0" title="data centre power monitoring" class="sf-depth-2 ">Zero Downtime</a></li><li id="menu-602-1" class="middle even sf-item-2 sf-depth-2 sf-no-children"><a href="/imeter/why-imeter" title="power and environmental monitoring" class="sf-depth-2 ">Why iMeter</a></li><li id="menu-610-1" class="middle odd sf-item-3 sf-depth-2 sf-no-children"><a href="/imeter/how-it-works-0" title="power and environmental monitoring" class="sf-depth-2 ">How it works</a></li><li id="menu-603-1" class="middle even sf-item-4 sf-depth-2 sf-no-children"><a href="/imeter/imeter-monitoring-software" title="power and environmental monitoring software" class="sf-depth-2 ">Monitoring software</a></li><li id="menu-611-1" class="last odd sf-item-5 sf-depth-2 sf-no-children"><a href="/imeter/case-studies" title="University of Nottingham opts for 2bm iMeter power and environmental monitoring solution" class="sf-depth-2 ">Case Studies</a></li></ul></li><li id="menu-565-1" class="last odd sf-item-7 sf-depth-1 sf-no-children"><a href="/contact-us" class="sf-depth-1 ">contact us</a></li></ul> </div> 
</div> 
    </div> 
      </nav> 
      <div id="headerimagepanel"> 
       <div id="headerimage"><img src="/sites/all/themes/bmco/images/newsheader.jpg" /></div> 
       <div id="headermenu"><nav id="hmenupanel" class="navigation"></nav></div> 
      </div> 
     </header> 
     <nav id="menupanel"> 
      <nav id="archivemenu" class="navigation"> <div class="region region-archivemenublog"> 
    <div id="block-views-blog-archive-block" class="block block-views"> 

    <h2>Monthly archive</h2> 

    <div class="content"> 
    <div class="view view-blog-archive view-id-blog_archive view-display-id-block view-dom-id-a384fd1f4628f80c660e22d4df3b4779"> 



     <div class="view-content"> 
     <div class="item-list"> 
    <ul class="views-summary"> 
     <li><a href="/blog/archive/201208">August 2012</a> 
       (1) 
      </li> 
    </ul> 
</div> 
    </div> 






</div> </div> 
</div> 
    </div> 
</nav> 
     </nav> 
     <div id="contentpanel"> 
             <h2 class="title" id="page-title">How secure are your racks? </h2> 

       <div class="region region-content"> 
    <div id="block-system-main" class="block block-system"> 


    <div class="content"> 
    <div id="node-68" class="node node-article node-promoted clearfix" about="/blog/how-secure-are-your-racks" typeof="sioc:Item foaf:Document"> 


    I need all this text below: 

    <div class="content"> 
    <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Data centre security is a big issue – especially for co-location centres hosting multiple racks for multiple, often competing, clients. Yet whilst security to access the data centre can often be impressive, individual rack level security is often inadequate. Given the number of in-house staff and external engineers, from cablers to storage and server providers, traipsing through a data centre on a near daily basis, poor rack level security is a potential risk.</p> 
<p>According to a recent survey conducted by Lieberman Software, 42 percent of IT staff can get unauthorised access to their organisation’s most sensitive information – including the CEO’s private documents. The failing is blamed on management’s naivety when it comes to understanding just how much privileged access their IT departments actually have.</p> 
<p>The fact that most racks are secured only with standard handles using a manual key, bears out this survey. Easily broken or bypassed, these locks provide minimal corporate protection; they offer no access control or audit trail of activity. Given the huge ongoing investment in data centres - during 2011-2012, the UK invested an estimated $3.35 billion in data centres; the second highest spending of any country, according to the Datacentre Dynamics Global Industry Census 2011 - extending standard access control techniques to the data centre racks is an important step.</p> 
<p>Companies can opt for a rack specific key, combination locks or key cards that are IP enabled to allow an organisation to impose strict control over the time/day an individual is allowed to access the rack. Using standard access control software, all activity is recorded and audited, providing the organisation with a complete list of those who have accessed the racks.</p> 
<p>For organisations, this approach adds control and addresses one important aspect of the internal threat. For co-location sites, rack level security removes the need to cage off client specific rack space areas freeing up space that can be used for more racks, delivering a return on investment, as well as improved client security.</p> 
</div></div></div> </div> 

답변

2

편집 (그것은 HTML의 페이지의 마지막 DIV이다). 전체 HTML을 기반으로, 나는 XPathQuery에게

변경 그런 다음

는 자식을 반복, 이것에 대한

-(NSString*) getStringForTFHppleElement:(TFHppleElement *)element { 

    NSMutableString *result = [NSMutableString new]; 

    // Iterate recursively through all children 
    for (TFHppleElement *child in [element children]) 
     [result appendString:[self getStringForTFHppleElement:child]]; 

    // Hpple creates a <text> node when it parses texts 
    if ([element.tagName isEqualToString:@"text"]) 
     [result appendString:element.content]; 

    return result; 
} 
+0

감사를 텍스트를 얻을 수있는 페이지의 : 마지막 "내용"하지만 그 나는 HTML의 부하를 붙여 넣을 didnt는 나의 질문에 바로 마지막 . 네 번째 "content"클래스의 텍스트는 어떻게 얻을 수 있습니까? – dev6546

+0

네 번째 div를 원한다면'element = [elements objectAtIndex : 3]' – nicolasthenoz

+0

아아, objectAtIndex가 없어졌습니다. 감사합니다. – dev6546