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

有人能给我示范一种更好的方法吗?

您正在遍历行,而不是包含的单元格。您需要同时执行这两项操作才能获得可用形式的单元格值:

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

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

另请参阅"如何避免在抓取时连接来自节点的所有文本"。

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

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"]

我希望它看起来更像你想要的。

相关内容

  • 没有找到相关文章

最新更新