2012-09-18 83 views
0

我正在使用hpple庫來獲取網頁上某個div的內容。查找某個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); 

現在,我想,應該填字符串*與DIV容器的內容串?

我得到這個錯誤和崩潰:

*終止應用程序由於未捕獲的異常 'NSRangeException',原因是:「* - [__ NSArrayM objectAtIndex:]:索引0超越界限的空數組'

現在我想這是因爲它沒有填充任何從該div的數組?一個原因是它沒有發現解析我給它的鏈接,但它沒有在xpathParser上崩潰,所以我猜它的解析它很好?

,告訴我它的我的XPath在這行是錯誤的:

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

誰能幫我找到的文本「我需要所有這些文字下方:」下面的DIV中的XPath(我提供所有上面的div(這是HTML的頁面上的最後一個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,我改變了給XPathQuery

TFHppleElement *element = [xpathParser peekAtSearchWithXPathQuery:@"//div[@id='node-68']"]; 
// Use peekAtSearchWithXPathQuery as there is only one vdiv with this id so you directly get the element (the function grabs the first element that corresponds to the XPath query) 

然後來獲取文本,通過其子迭代

-(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的負載。我將如何從第4個「內容」課獲取文本? – dev6546

+0

如果你知道你想要第四個div,只要做'element = [elements objectAtIndex:3]' – nicolasthenoz

+0

啊我錯過了objectAtIndex,謝謝。 – dev6546