2017-03-26 26 views
0

我試圖從下面的結構中提取HTML中的日期和文本。我正在使用goquery來做到這一點。將selector.Find()的範圍限制爲所選元素

<body> 
<div class="wrap"> 
    <div class="cont"> 
     <div class="cont_block"> 
      <p class="date"> 
       <font> 
        <font>Saturday, Apr 16,2016</font> 
       </font> 
      </p> 
      <div class="block_table"> 
       <table class="left" width="auto" height="auto" border="0" cellpadding="0" cellspacing="0"> 
        <tbody> 
         <tr> 
          <td class="left_top2"></td> 
          <td width="auto" class="bg_color2" height="12"></td> 
          <td class="right_top2"></td> 
         </tr> 
         <tr> 
          <td height="auto" class="right_mid2"></td> 
          <td class="bg_color2"> 
           <font> 
            <font>Loerem ipsum dolor sit amet</font> 
           </font> 
          </td> 
          <td class="bg_color2" width="14"></td> 
         </tr> 
         <tr> 
          <td class="left_bottom2"></td> 
          <td class="bg_color2"></td> 
          <td class="right_bottom2"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
     </div> 

     <div class="cont_block"> 
      <p class="date">Friday,Dec 18,2015</p> 
      <div class="block_table"> 
       <table class="right" width="auto" height="auto" border="0" cellpadding="0" cellspacing="0"> 
        <tbody> 
         <tr> 
          <td class="left_top3"></td> 
          <td width="auto" class="bg_color3" height="12"></td> 
          <td class="right_top3"></td> 
         </tr> 
         <tr> 
          <td height="auto" class="bg_color3" width="14">&nbsp;</td> 
          <td class="bg_color3">Loerem ipsum dolor sit amet</td> 
          <td class="right_mid3"></td> 
         </tr> 
         <tr> 
          <td class="left_bottom3"></td> 
          <td class="bg_color3"></td> 
          <td class="right_bottom3"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
      <div class="block_table"> 
       <table class="right" width="auto" height="auto" border="0" cellpadding="0" cellspacing="0"> 
        <tbody> 
         <tr> 
          <td class="left_top3"></td> 
          <td width="auto" class="bg_color3" height="12"></td> 
          <td class="right_top3"></td> 
         </tr> 
         <tr> 
          <td height="auto" class="bg_color3" width="14">&nbsp;</td> 
          <td class="bg_color3">Loerem ipsum dolor sit amet</td> 
          <td class="right_mid3"></td> 
         </tr> 
         <tr> 
          <td class="left_bottom3"></td> 
          <td class="bg_color3"></td> 
          <td class="right_bottom3"></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
     </div> 
    </div> 
</div> 

我曾嘗試做這個例子很多:

doc.Find(".wrap .cont .cont_block").Each(func(i int, s *goquery.Selection) { 
    fmt.Println(s.Find(".date").Text()) 
    s.Find(".block_table td").Each(func(j int, c *goquery.Selection){ 
     if c.Text() != "" { 
      fmt.Println(c.Text()) 
     } 
    }) 
}) 

的問題是,在找到的日期和文本返回的結果是外返回的結果.cont_block的範圍。基本上它會在每次迭代中返回文檔中當前選中的.cont_block下的所有日期和td。

我錯過了什麼?

+1

我不明白你的問題。我運行了你的代碼,看起來它正在做你所要求的。你能否舉一個你得到的例子,以及它與你的期望有什麼不同? – Zoyd

+0

你很對。我正在處理的html文件相當大,結果證明它們無效。他們有導致問題的未封閉元素。 –

回答

0

問題是我正在處理的HTML文件中未封閉的元素。