Reputation: 1070
This is the specific bit of code I am using for the scrape:
require 'singleton'
require 'open-uri'
class ProgramHighlights < ActiveRecord::Base
self.table_name = 'program_highlights'
include ActiveRecord::Singleton
def fetch
url = "http://kboo.fm/"
doc = Nokogiri::HTML(open(url))
titles = []
program_title = doc.css(".title a").each do |title|
titles.push(title)
end
end
end
When accessing the titles array and eaching through it my output is:
(Element:0x5b40910 {
name = "a",
attributes = [
#(Attr:0x5b8c310 {
name = "href",
value = "/content/thedeathsofothersthefateofciviliansinamericaswars"
}),
#(Attr:0x5b8c306 {
name = "title",
value = "The Deaths of Others: The Fate of Civilians in America's Wars"
})],
children = [
#(Text "The Deaths of Others: The Fate of Civilians in America's Wars")]
})
I specifically want to get "value" However doing the following does not pull it:
titles[0].value
titles[0]["value"]
titles[0][value]
I've no idea why I cannot access it since it is seemingly a hash. Any pointers of a direction to go with this? I can't get the data in a simple JSON format, hence the need for the scrape.
Upvotes: 0
Views: 163
Reputation: 46836
To get the attribute value of a node, you can use ['attribute_name']. For example:
require 'nokogiri'
html = %Q{
<html>
<a href="/content/thedeathsofothersthefateofciviliansinamericaswars" title="The Deaths of Others: The Fate of Civilians in America's Wars">
</html>
}
doc = Nokogiri::HTML(html)
node = doc.at_css('a')
puts node['href']
#=> /content/thedeathsofothersthefateofciviliansinamericaswars
puts node['title']
#=> The Deaths of Others: The Fate of Civilians in America's Wars
Assuming you want the title attribute value of each link, you can do:
program_title = doc.css(".title a").each do |link|
titles.push(link['title'])
end
Upvotes: 1