rubyhpricot

how to extract onclick in Hpricot ruby


i want to get the parentID from this.

<div class="module-head" onclick="getURL('/DU/initAction.do?parentId=1063&categoryType=6')"></div>

how can i achieve this?


Solution

  • The setup:

    html = <<-EOF
    <div class="module-head" onclick="getURL('/DU/initAction.do?parentId=1063&categoryType=6')"></div>
    EOF
    

    Hpricot:

    doc = Hpricot html
    

    or Nokogiri:

    doc = Nokogiri::HTML html
    

    Interestingly, it's the same in both:

    doc.at('div')[:onclick][/parentId=(\d+)/, 1]
    #=> "1063"