使用Nokogiri在BR标签上拆分内容

Mar*_*tto 6 ruby xpath parsing screen-scraping nokogiri

我有一段代码我试图用nokogiri解析,看起来像这样:

<td class="j">
    <a title="title text1" href="http://link1.com">Link 1</a> (info1), Blah 1,<br>
    <a title="title text2" href="http://link2.com">Link 2</a> (info1), Blah 1,<br>
    <a title="title text2" href="http://link3.com">Link 3</a> (info2), Blah 1 Foo 2,<br>
</td>
Run Code Online (Sandbox Code Playgroud)

我可以使用以下内容访问td.j的源代码: data_items = doc.css("td.j")

我的目标是将每一行分成一组哈希.我能看到的唯一逻辑分裂点是拆分BR,然后在字符串上使用一些正则表达式.

我想知道是否有一个更好的方法来做这个可能只使用nokogiri?即使我可以使用nokogiri来吸取3行项目,它也会让我更容易,因为我可以在.content结果上进行一些正则表达式解析.

不知道如何使用Nokogiri抓住以br结尾的行 - 我应该使用xpath吗?任何方向表示赞赏!谢谢

the*_*Man 17

我不确定使用哈希数组的意义,如果没有一个例子,我无法提出一些建议.但是,为了在<br>标签上分割文本,我会这样做:

require 'nokogiri'

doc = Nokogiri::HTML('<td class="j">
    <a title="title text1" href="http://link1.com">Link 1</a> (info1), Blah 1,<br>
    <a title="title text2" href="http://link2.com">Link 2</a> (info1), Blah 1,<br>
    <a title="title text2" href="http://link3.com">Link 3</a> (info2), Blah 1 Foo 2,<br>
</td>')

doc.search('br').each do |n|
  n.replace("\n")
end
doc.at('tr.j').text.split("\n") # => ["", "    Link 1 (info1), Blah 1,", "Link 2 (info1), Blah 1,", "Link 3 (info2), Blah 1 Foo 2,"]
Run Code Online (Sandbox Code Playgroud)

这将使您更接近哈希:

Hash[*doc.at('td.j').text.split("\n")[1 .. -1].map{ |t| t.strip.split(',')[0 .. 1] }.flatten] # => {"Link 1 (info1)"=>" Blah 1", "Link 2 (info1)"=>" Blah 1", "Link 3 (info2)"=>" Blah 1 Foo 2"}
Run Code Online (Sandbox Code Playgroud)


mu *_*ort 1

如果您的数据确实是规则的,并且您不需要元素的属性,那么您可以解析每个表格单元格的文本形式,而根本<a>不必担心元素。<br>

给定一些像这样的 HTML html

<table>
    <tbody>
        <tr>
            <td class="j">
                <a title="title text1" href="http://link1.com">Link 1</a> (info1), Blah 1,<br>
                <a title="title text2" href="http://link2.com">Link 2</a> (info1), Blah 1,<br>
                <a title="title text2" href="http://link3.com">Link 3</a> (info2), Blah 1 Foo 2,<br>
            </td>
            <td class="j">
                <a title="title text1" href="http://link4.com">Link 4</a> (info1), Blah 2,<br>
                <a title="title text2" href="http://link5.com">Link 5</a> (info1), Blah 2,<br>
                <a title="title text2" href="http://link6.com">Link 6</a> (info2), Blah 2 Foo 2,<br>
            </td>
        </tr>
        <tr>
            <td class="j">
                <a title="title text1" href="http://link7.com">Link 7</a> (info1), Blah 3,<br>
                <a title="title text2" href="http://link8.com">Link 8</a> (info1), Blah 3,<br>
                <a title="title text2" href="http://link9.com">Link 9</a> (info2), Blah 3 Foo 2,<br>
            </td>
            <td class="j">
                <a title="title text1" href="http://linkA.com">Link A</a> (info1), Blah 4,<br>
                <a title="title text2" href="http://linkB.com">Link B</a> (info1), Blah 4,<br>
                <a title="title text2" href="http://linkC.com">Link C</a> (info2), Blah 4 Foo 2,<br>
            </td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

你可以这样做:

chunks = doc.search('.j').map { |td| td.text.strip.scan(/[^,]+,[^,]+/) }
Run Code Online (Sandbox Code Playgroud)

并有这个:

[
    [ "Link 1 (info1), Blah 1", "Link 2 (info1), Blah 1", "Link 3 (info2), Blah 1 Foo 2" ],
    [ "Link 4 (info1), Blah 2", "Link 5 (info1), Blah 2", "Link 6 (info2), Blah 2 Foo 2" ],
    [ "Link 7 (info1), Blah 3", "Link 8 (info1), Blah 3", "Link 9 (info2), Blah 3 Foo 2" ],
    [ "Link A (info1), Blah 4", "Link B (info1), Blah 4", "Link C (info2), Blah 4 Foo 2" ]
]
Run Code Online (Sandbox Code Playgroud)

chunks。然后你可以将其转换为你需要的任何哈希形式。