Search code examples
ruby-on-railsrubyhpricot

Hpricot: how to do conditional search using Hpricot in Ruby on Rails


I am parsing two different sites having similar HTML tags. I need to use a common parser for this. My issue is one site has a HTML format div/ol/li/span/a and other has div/ol/li/h3/a

My current parser code is

 doc = Hpricot(open("http://test.com").read)
 doc.search("div/ol/li/span/a").each do |a|
   question = a.inner_html
   ans_url =  a.attributes['href']
   puts question
   puts answer_url
 end

This works well with the first site. How can I use this same code to parse my second site(div/ol/li/h3/a). How can I specify conditions. What I tried is shown below

 doc = Hpricot(open("http://test.com").read)
 doc.search("div/ol/li/span or h3/a").each do |a|
   question = a.inner_html
   ans_url =  a.attributes['href']
   puts question
   puts answer_url
 end

But this didnot work. Can anyone please help.


Solution

  • It worked I used the below code

     doc.search("div/ol/li/span/a | div/ol/li/h3/a").each do |a|
      #..
     end
    

    Thanks all