2017-01-03 4 views
0
<div class="seperate"> 
    <h2>Public info</h2> 
    <p> 
     <strong>Property type:</strong> Semi-detached house | 
     <strong>Tenure:</strong> Leasehold | 
     <strong>Last sale:</strong> £71,000 | <strong>Sale date:</strong> 5th Dec 2007 - <a href="" class="toggle_sold_prices">Previous sales</a> 
     <span id="sold-prices" class="none"> 
         <br> 
          <strong>Property type:</strong> 
          Semi-detached house | 
          <strong>Tenure:</strong> 
          Leasehold | 
         <strong>Previous sale:</strong> £75,000 | 
         <strong>Sale date:</strong> 
    3rd Oct 2006 
         <br> 
          <strong>Property type:</strong> 
          Semi-detached house | 
          <strong>Tenure:</strong> 
          Leasehold | 
         <strong>Previous sale:</strong> £36,000 | 
         <strong>Sale date:</strong> 
    26th Sep 2002 
         <br> 
          <strong>Property type:</strong> 
          Semi-detached house | 
          <strong>Tenure:</strong> 
          Leasehold | 
         <strong>Previous sale:</strong> £39,950 | 
         <strong>Sale date:</strong> 
    27th Jan 1995 
          <span class="new-build">New build</span> 
     </span> 
     | <a href="/for-sale/details/42175871"><i class="icon icon-home nolink"></i>Currently for sale</a> 
    </p> 
</div> 

I 값 "현재 판매" "마지막 세일", "판매 날짜"를위한 데이터를 긁어하려고를 무시하고 데이터를 긁어하는 방법 내가 할 수있는 일</p> <pre><code><span id="sold-prices" class="none"> </code></pre> <p>내가 아는 내부에 모든 것을 제외하고 내장 된 태그

html.search(".//div[@class='separate']") 

별도의 div 안에 HTML을 가져 오는 것이지만 원하는 태그의 데이터를 어떻게 다듬을 수 있는지 잘 모릅니다. 어떤 아이디어?

+0

사이에서 데이터를 가져 오는 방법에 대해 마지막 판매 :'및'| '? 'nokogiri '는 도움이되지 않습니다. 문자열 조작 도구를 사용해 본 경험이 필요합니다. – num8er

+0

@ num8er, 시도해보고 업데이트를 게시 해주세요 – Raaz

+0

"[mcve]"을 (를) 읽으십시오. 질문 할 때 문제를 시연하고 문제를 해결하기 위해보다 실질적인 시도를하는 데 필요한 최소한의 정보 만 입력해야합니다. 이는 우리가 시도를 반복하거나 질문을 맞추기 위해 시간을 낭비하는 것을 피하는 데 도움이됩니다. –

답변

2

Nokogiri가 HTML 처리를 마치면 노드를 찾고 조작하기가 정말 쉽습니다. 때로는 노드를 선택적으로 제거하여 DOM을 단순화하는 것을 의미합니다. 이것은 그 시간 중 하나입니다

require 'nokogiri' 
doc = Nokogiri::HTML(<<EOT) 
<div class="seperate"> 
    <p> 
    <strong>Property type:</strong> Semi-detached house | 
    <strong>Tenure:</strong> Leasehold | 
    <strong>Last sale:</strong> £71,000 | <strong>Sale date:</strong> 5th Dec 2007 - <a href="" class="toggle_sold_prices">Previous sales</a> 
    <span id="sold-prices" class="none"> 
     <br> 
      <strong>Property type:</strong> 
      Semi-detached house | 
      <strong>Tenure:</strong> 
      Leasehold | 
    </span> 
    </p> 
</div> 
EOT 

doc.at('#sold-prices').remove 
data = doc.search('strong').map{ |strong| 
    [strong.text, strong.next_sibling.text.tr('|', '').strip] 
}.to_h 

data # => {"Property type:"=>"Semi-detached house", "Tenure:"=>"Leasehold", "Last sale:"=>"£71,000", "Sale date:"=>"5th Dec 2007 -"} 

트릭은 다음과 같습니다 숲을 없애는

doc.at('#sold-prices').remove 

그래서 당신은 당신이 원하는 나무를 볼 수 있습니다.

결과 데이터를 정리하는 데 약간 더 필요한 것이 있지만 나머지 코드는 설명이 필요하므로 쉽게 조정할 수 있습니다.