我正在尝试使用以下结构从 HTML 中提取日期和文本。我正在使用 goquery 来执行此操作。
<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"> </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"> </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。
我错过了什么?