Ruby on Rails Wednesday, May 1, 2013

Hi,

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:

========================================================
#Google laptop search

require "nokogiri"
require "open-uri"
result = open("http://www.google.co.in/search?q=laptop", "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 result.inspect
doc = Nokogiri::HTML(result)

puts "result list"
#puts doc
puts doc.css("#resultStats").text

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?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment