ruby - 使用 ruby​​ mechanize 遍历 html 元素

标签 ruby arrays web-scraping mechanize

即使在我尝试做的事情上,我也可能离我很远,但这是我得到的。

我想收集梦幻足球投影并将所有 html 元素(针对不同球员)放入一个数组中并遍历它们以显示结果。

require 'mechanize'

mechanize = Mechanize.new

dk_qb = mechanize.get('http://www.numberfire.com/nfl/fantasy/fantasy-football-projections/qb')



dk_qb_array = ['#container > div > div > div:nth-child(2) > div.fl.clearfix > h2',
            '#container > div > div > div:nth-child(3) > div.fl.clearfix > h2']

dk_qb_array.each do |name|
 require 'mechanize'
 mechanize = Mechanize.new

 dk_qb = mechanize.get('http://www.numberfire.com/nfl/fantasy/fantasy-football-projections/qb')

 puts "#{dk_qb}.at('#{name}').text.strip"

结尾
returns ==> #<Mechanize::Page:0x007f9ed95058f0>.at('#container > div > div > div:nth-child(2) > div.fl.clearfix > h2').text.strip

            #<Mechanize::Page:0x007f9ed91382e0>.at('#container > div > div > div:nth-child(3) > div.fl.clearfix > h2').text.strip

我一次一个地工作,但任何关于迭代更多元素的建议将不胜感激。

最佳答案

你不需要在这里再次要求 Mechanize :

dk_qb_array.each do |name|
  require 'mechanize'

无论如何,您应该使用 Nokogiri——而不是 Mechanize:
$ gem install nokogiri

然后:
require 'nokogiri'
require 'open-uri'

selectors = [
  '#container > div > div > div:nth-child(2) > div.fl.clearfix > h2',
  '#container > div > div > div:nth-child(3) > div.fl.clearfix > h2',
]
url = 'http://www.numberfire.com/nfl/fantasy/fantasy-football-projections/qb'
doc = Nokogiri::HTML(open(url))

selectors.each do |selector|
  puts selector
  doc.css(selector).each do |matching_tag|
    puts "\t #{matching_tag.text}"
  end
end


--output:--
#container > div > div > div:nth-child(2) > div.fl.clearfix > h2
     Week 1 Fantasy Football QB Projections
#container > div > div > div:nth-child(3) > div.fl.clearfix > h2

从输出中可以看出,第二个选择器没有匹配项;并且您的第一个选择器的单个匹配可能不是您想要的。只有一个 <h2>在整个页面上,所以寻找第二个是行不通的。

更好的方法是使用 id 属性直接进入您想要的区域,例如
"tbody#projection-data > tr"

然后做这样的事情:
doc.css("tbody#projection-data > tr").each do |tr|
  #The <tr> contains the data for one player

  tr.css('td').each do |td|  #Now step through the <td>'s for the given <tr>/player
    puts td.text.strip
  end

  puts '-' * 10  #Marks the end of the data for one <tr>/player

  #Now, loop back up and get the next <tr>/player
end


--output:--
Drew Brees (QB, NO)
ATL
#26
1
1
27.49/40.64
335.01
3.07
0.71
2.84
10.42
0.06
17.4-33.58
25.49
29.33
$0
0
26.25
$0
0
25.54
$0
0
25.54
$0
0
26.25
$0
0
----------
Peyton Manning (QB, DEN)
...
...

关于ruby - 使用 ruby​​ mechanize 遍历 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25315961/

相关文章:

ruby - 如何使用范围 (..) 运算符查找从 "String"到 "\n\n"的行?

ruby-on-rails - 如何使用 Ruby Pry 跳出循环?

javascript - 将特定数组值合并为单个值

ruby-on-rails - 未定义的方法 `click' 用于 "2":String, 使用 Mechanize 时出现 Rails 错误

r - 使用 rvest 在 R 中进行 Web 抓取并找到 html_note

ruby-on-rails - 如何在 Rails 中查找 ID 从一个数字到另一个数字的所有对象?

ruby-on-rails - 无法在 Ruby on Rails 中创建数据库

arrays - 如何从两个向量元素的所有可能组合中获取单个数组?

javascript - 在 Javascript 中,为什么空数组的最小值是无穷大?

javascript - 为什么 requests.get() 使用 Python 检索的 HTML 与浏览器不同?