I am learning “Nokogiri”. But I am quite good Nokogiri’s #xpath
technique.But wanted to learn also #css. So started to search online
materials to see how other writes a code using CSS techniques. Doing so
I got one interesting code as below:
require “nokogiri”
require “open-uri”
result = open(“laptop - Google Search”, “User-Agent” =>
“HTTP_USER_AGENT:Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)
AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.47
Safari/534.13”).read()
puts “related searches”
doc.css(“#trev a”).each do |x|
puts “#{x.text} => #{x[:href]}”
end
puts “results”
doc.css(“#ires ol li”).each do |x|
node = x.at_css(“div a”)
puts node[:href]
puts node.text
desc_node = x.at_css(“div div.s”)
puts node.text
end
But I am not comfortable with some notation used here,so thought if
anyone out there help me to redirect to a resource where I can learn
such techniques.
My hard confusion is with the below lines:
***** “#ires ol li” *****
when to write this way?
what # stands for? Looking at the source page I saw ires is one
id. – when I need to use such # and when .?
Is there any other operators like # and . when using css?
On Wednesday, May 1, 2013 1:43:06 PM UTC+1, Ruby-Forum.com User wrote:
But I am not comfortable with some notation used here,so thought if
anyone out there help me to redirect to a resource where I can learn
such techniques.
You need to read up on css selectors
My hard confusion is with the below lines:
***** “#ires ol li” *****
when to write this way?
what # stands for? Looking at the source page I saw ires is one
id. – when I need to use such # and when .?
#foo finds an element with id foo whereas .foo finds an element with
class
foo. Which one to use depends on the markup.
Is there any other operators like # and . when using css?
Yes, there are a whole bunch, such as the pseudo selectors (:not,
:nth-child), attribute selectors etc.
@Fred Thank you very much! The source link you shared is exactly what I
was looking for. For Nokogiri - I think only the selectors part would be
great to read to get start Nokogiri right?