将selector.Find()的范围限制为所选元素

时间:2017-03-26 01:25:10

标签: go goquery

我正在尝试使用以下结构从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">&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 个答案:

答案 0 :(得分:0)

问题是我正在处理的HTML文件中未关闭的元素。