2015-06-03 33 views
0

我試圖從使用nokogiri和遏制網站抓取信息,但我似乎無法找到正確的名稱/標題,以找出在哪裏刮(我試圖刮api鍵,這是在該代碼的底部爲「xxxxxxx」),甚至如何,請幫助,謝謝。 :)我可以讓Nokogiri從Ruby跨度中刮取文本嗎?

的HTML代碼低於:

<body class="html not-front logged-in no-sidebars page-app page-app- page-app-8383900 page-app-keys i18n-en" data-twttr-rendered="true"> 

<div id="skip-link"></div> 
<div id="page-wrapper"> 
    <!-- 

    Code for the global nav 

    --> 
    <nav id="globalnav" class="without-subnav"></nav> 
    <nav id="subnav"></nav> 
    <section id="hero" class="hero-short"></section> 

<div class="container"> 
    ::before 
    <div id="messages"></div> 
    <div id="gaz-content-wrap-outer" class="row"> 
     ::before 
     <div id="gaz-content-wrap-inner" class="span12"> 
      <div class="row"> 
       ::before 
       <div class="article-wrap span12"> 
        <article id="gaz-content-body" class="content"> 
         <header></header> 
         <div class="header-action"></div> 
         <div class="tabs"></div> 

小姑娘= 「d區d - 嵌段 - 系統G-主」>

<div class="app-details"> 
    <h2> 

     Application Settings 

    </h2> 
    <div class="description"></div> 
    <div class="app-settings"> 
     <div class="row"> 
      ::before 
      <span class="heading"> 

       Consumer Key (API Key) 

      </span> 
      <span> 

       xxxxxxxxx 

      </span> 

所有我似乎可以得到的是「內容」文本。

我的代碼如下所示:

consumer = html.at("#gaz-content-body")['class'] 
puts consumer 

我不知道什麼類型選擇類或跨越然後輸入文本。我能得到的是nokogiri把「內容」。

回答

1

在這種情況下,我們需要找到類'標題'跨度後的第二個跨度,並在類「app-settings」的div內找到(只是有點泛泛但不是太多)。我使用.search而不是.at來檢索兩個跨度並獲得第二個跨度。

# Gets the 2 span elements under <div class='app-settings'>. 
res = html.search('#gaz-content-body .app-settings span') 

# Use .text to get the contents of the 2nd element. 
res[1].text.strip 
# => "xxxxxxxx" 

但你也可以使用.at瞄準同用:

res = html.at("#gaz-content-body .app-settings span:nth-child(2)") 
res.text.strip 
# => "xxxxxxxx" 
+0

這些都不似乎工作,我要麼得到一個空白的輸出或錯誤閱讀「未定義的方法'文本」的零: NilClass(NoMethodError)「 – marriedjane875

+0

對不起,它應該適用於您爲文檔提供的部分。你介意提供整個文件嗎? – limekin

+0

是的,1秒,現在就做......完成 – marriedjane875

相關問題