我的代码:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
PAGE_URL = "http://www.whoscored.com/Teams/1799/Fixtures/Spain-Almeria"
page = Nokogiri::HTML(open(PAGE_URL))
CSS_SELECTOR = "a.match-link.match-report.rc"
links = page.css(CSS_SELECTOR)
puts links.length # => RESULT = 0
puts links[0].text # => RESULT = none
puts links[0]["href"] # => RESULT = ./read_stats.rb:15:in `<main>': undefined method `text' for nil:NilClass (NoMethodError)
结果应该是:
1
Match Report
/Matches/738463/MatchReport
但我的结果是:
0
./read_stats.rb:15:in `<main>': undefined method `text' for nil:NilClass (NoMethodError)
它没有用,我也没有看到问题...
感谢。
答案 0 :(得分:2)
您需要在抓取页面之前渲染页面。
require 'watir-webdriver'
require 'nokogiri'
$browser = Watir::Browser.start "http://www.whoscored.com/Teams/1799/Fixtures/Spain-Almeria"
$page_html = Nokogiri::HTML.parse($browser.html)
$page_html.css("td[@class='toolbar right']").each do |me|
print "#{me.count}\n#{me.text}\n#{me.css("a").map{|link| link['href']}[0]}\n\n"
end