草庐IT

go - 将 selector.Find() 的范围限制为被选元素

coder 2024-07-10 原文

我正在尝试使用以下结构从 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。

我错过了什么?

最佳答案

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

关于go - 将 selector.Find() 的范围限制为被选元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43023935/

有关go - 将 selector.Find() 的范围限制为被选元素的更多相关文章

  1. ruby - 触发器 ruby​​ 中 3 点范围运算符和 2 点范围运算符的区别 - 2

    请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是

  2. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  3. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  4. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  5. ruby - 在哈希的键数组中追加元素 - 2

    查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用

  6. 「Python|Selenium|场景案例」如何定位iframe中的元素? - 2

    本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决

  7. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  8. ruby - Hanami link_to 助手只呈现最后一个元素 - 2

    我是HanamiWorld的新人。我已经写了这段代码:moduleWeb::Views::HomeclassIndexincludeWeb::ViewincludeHanami::Helpers::HtmlHelperdeftitlehtml.headerdoh1'Testsearchengine',id:'title'hrdiv(id:'test')dolink_to('Home',"/",class:'mnu_orizontal')link_to('About',"/",class:'mnu_orizontal')endendendendend我在模板上调用了title方法。htm

  9. ruby-on-rails - ActiveRecord 的 find_or_create* 方法是否存在根本性缺陷? - 2

    有几种方法:first_or_create_by、find_or_create_by等,它们的工作原理是:与数据库对话以尝试找到我们想要的东西如果我们找不到,就自己做保存到数据库显然,并发调用这些方法可能会使两个线程都找不到它们想要的东西,并且在第3步中一个线程会意外失败。似乎更好的解决方案是,创建或查找即:提前在您的数据库中创建合理的唯一性约束。如果你想保存一些东西,就保存它如果有效,那就太好了。如果它因为RecordNotUnique异常而无法工作,它已经存在,太好了,加载它那么在什么情况下我想使用Rails内置的东西而不是我自己的(看起来更可靠)create_or_find?

  10. Ruby 日期参数超出范围 - 2

    我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby​​中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天

随机推荐