Parse table using Nokogiri

后端 未结 3 1289
一向
一向 2021-01-06 13:16

I would like to parse a table using Nokogiri. I\'m doing it this way

def parse_table_nokogiri(html)

    doc = Nokogiri::HTML(html)

    doc.search(\'table &         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-06 14:19

    Simple (but not DRY) way of using alternation:

    require 'nokogiri'
    
    doc = Nokogiri::HTML <
    NOT THIS
    foo bar
    ENDHTML p doc.xpath( '//table/tr/td/text()|//table/tr/td/font/text()' ) #=> [#, #=> #]

    See XPath with optional element in hierarchy for a more DRY answer.

    In this case, however, you can simply do:

    p doc.xpath( '//table/tr/td//text()' )
    #=> [#,
    #=>  #]
    

    Note that your table structure (and mine above) which does not have an explicit tbody element is invalid for XHTML. Given your explicit table > tr above, however, I assume that you have a reason for this.

提交回复
热议问题