Nokogiri刮文字方法的替代方案?

时间:2013-11-19 01:08:16

标签: css ruby-on-rails ruby nokogiri

我正试图让Nokogiri为了林书豪的最后一场比赛数据而刮掉ESPN的网站,然而,CSS文本方法给了我一个字符串之间没有任何空格的字符串。

scraper.get_last_game_stats.text返回的字符串是:

"Sat 11/16vsDENW 122-111326-11.5450-2.0004-6.66747113116Wed 11/13@ PHIL 117-1234910-19.5269-15.6005-6.833512005834Sat 11/9vsLACL 94-107263-7.4290-0.0000-0.0001701156"

我试图在每个统计数据之间放置空格,但是,即使我循环通过主要对象,在迭代之间放置空格或破折号,我也无法分割数字,用于抢断,数据块,积分,失误等等:

class PlayerScraper
  attr_accessor :player_data, :name

  def initialize(url)
    @player_data = Nokogiri::HTML(open(url))
  end

  def get_last_game_stats
    @last_game_stats = @player_data.css('tr[class^="oddrow team-46"]')
  end
end

jlin_url = "http://espn.go.com/nba/player/_/id/4299/jeremy-lin"

scraper = PlayerScraper.new(jlin_url)
scraper.get_last_game_stats.text

有人能告诉我一个更好的方法吗?

3 个答案:

答案 0 :(得分:2)

你走的是行,但没有包含的单元格。您需要同时执行这两项操作以获取可用形式的单元格值:

require 'open-uri'
require 'nokogiri'

URL = 'http://espn.go.com/nba/player/_/id/4299/jeremy-lin'
doc = Nokogiri::HTML(open(URL))

data = doc.css('tr[class^="oddrow team-46"]').map{ |tr|
  tr.css('td').map(&:text)
}

data
# => [["Sat 11/16",
#      "vsDEN",
#      "W 122-111",
#      "32",
#      "6-11",
#      ".545",
#      "0-2",
#      ".000",
#      "4-6",
#      ".667",
#      "4",
#      "7",
#      "1",
#      "1",
#      "3",
#      "1",
#      "16"],
#     ["Wed 11/13",
#      "@ PHI",
#      "L 117-123",
#      "49",
#      "10-19",
#      ".526",
#      "9-15",
#      ".600",
#      "5-6",
#      ".833",
#      "5",
#      "12",
#      "0",
#      "0",
#      "5",
#      "8",
#      "34"],
#     ["Sat 11/9",
#      "vsLAC",
#      "L 94-107",
#      "26",
#      "3-7",
#      ".429",
#      "0-0",
#      ".000",
#      "0-0",
#      ".000",
#      "1",
#      "7",
#      "0",
#      "1",
#      "1",
#      "5",
#      "6"]]

以不同的方式查看数据,将其输出为行:

data.each do |row|
  puts row.join(', ')
end
# >> Sat 11/16, vsDEN, W 122-111, 32, 6-11, .545, 0-2, .000, 4-6, .667, 4, 7, 1, 1, 3, 1, 16
# >> Wed 11/13, @ PHI, L 117-123, 49, 10-19, .526, 9-15, .600, 5-6, .833, 5, 12, 0, 0, 5, 8, 34
# >> Sat 11/9, vsLAC, L 94-107, 26, 3-7, .429, 0-0, .000, 0-0, .000, 1, 7, 0, 1, 1, 5, 6

表非常简单,可以使用两个嵌套循环创建。要稍后访问每个单元格,您需要执行相同操作,在循环中遍历行,并在该循环内部,遍历单元格。这就是我写的所有代码。

另请参阅“How to avoid joining all text from Nodes when scraping”。

答案 1 :(得分:1)

我认为您应该阅读tr元素,然后循环其HTML内容并分别处理每个td,否则使用text方法和Rails HTML标记清理您将获得来自原始数据的混乱。

答案 2 :(得分:1)

text方法汇总所有选定节点的文本。尝试像

这样的东西
scraper.get_last_game_stats.map(&:text)

如果您希望单独评估tr个节点。当我这样做的时候,你指的是我得到的网址:

["Sat 11/16", "vsDEN", "W 122-111", "32", "6-11", ".545", "0-2", ".000", "4-6", ".667", "4", "7", "1", "1", "3", "1", "16", "Wed 11/13", "@ PHI", "L 117-123", "49", "10-19", ".526", "9-15", ".600", "5-6", ".833", "5", "12", "0", "0", "5", "8", "34", "Sat 11/9", "vsLAC", "L 94-107", "26", "3-7", ".429", "0-0", ".000", "0-0", ".000", "1", "7", "0", "1", "1", "5", "6"]

我希望看起来更像你正在寻找的东西。