I know this question has been asked earlier but I am not able to get the parsed result. I am trying to parse metawords using nokogiri, can any one point out my mistake?
keyword = []
meta_data = doc.xpath('//meta[@name="Keywords"]/@content') #parsing for keywords
meta_data.each do |meta|
keyword << meta.value
end
key_str=keyword.join(",")
I tried running this in irb as well but keyword returns a nil.
This is how I used it in irb
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML("www.google.com")
have already tried alternatives from other stackoverflow posts like
Nokogiri html parsing question but of no use, they still return nil. I guess i am doing something wrong somewhere.
http://www.google.com does not have any meta keywords in the source. View Source on the page to see for yourself. So even if everything else went perfectly, you’d still get no results there.
The result of
doc = Nokogiri::HTML("www.google.com")isIf you want to fetch the contents of a URL, you want to use something like:
If you get a valid HTML page, and use the proper casing on
keywordsto match the source, it works fine. Here’s an example from my IRB session, fetching a page from one of the apps on my site that happens to usename="keywords"instead ofname="Keywords":