I’m trying to parse this website using Ruby and Nokogiri:
Here’s my code:
require 'nokogiri'
require 'open-uri'
class StreamsController < ApplicationController
def index
end
def updateall
doc = Nokogiri::HTML(open('http://www.own3d.tv/game/League+of+Legends'))
# Grab all the Live streams from the front page.
doc.css('div#top_live .VIDEOS-1grid-box').each do |stream|
s = Stream.new
# Parse the URL.
s.url = stream.css('a.small-tn')['href']
end
end
end
On the # Parse the URL bit, I get the error Cannot convert String to Integer.
I’m kind of confused on how to use Nokogiri for this simple use case.
How can I get the href attribute of each link inside each |stream| object?
stream.css('a.small-tn')will return a collection of nodes. So calling['href']on the collection isn’t going to work since the collection acts as an array, and it thinks you’re trying to access an element at a certain index (hence the error). Rather, you need to decide if you want to iterate through them, or just grab the first:If you want to make this a bit more safe, you can check for nils:
Or you can use the
at_csshelper (as @AJcodez) pointed out, which does the same thing.