Can I use Hpricot to parse data into different array elem?

Discussion in 'Ruby' started by Christiaan Venter, May 21, 2009.

  1. Hi,

    I have a question about using Hpricot.

    Say I have a file search.html that contains the following:

    <ul class="search-results-item">
    <li class="search-item-title">


    <a
    href="http://www.sheetmusicplus.com/title/Holst-G-Wind-Quintet-Set/5015093">Holst
    G /Wind Quintet (Set)</a>
    </li>

    <span class="price">$21.95</span>

    <li class="search-item-title">


    <a
    href="http://www.sheetmusicplus.com/title/Holst-G-Wind-Quintet-Score/5014486">Holst
    G /Wind Quintet(Score)</a>
    </li>

    <span class="price">$15.95</span>

    And I want to use Hpricot to parse it and have each price matched with
    its title.

    If I do something like this

    doc = open("F:/search.html") { |f| Hpricot(f) }
    doc.search(".price").each do |price|
    prices = [price.inner_text]
    puts prices[0]

    =>$21.95
    $15.95

    Then it seems that my search results are in an array, but that both
    prices are in a single element in the array because both come up when I
    ask for the element at [0]
    I'm a ruby newbie--what am I missing?
    Thanks very much
    --
    Posted via http://www.ruby-forum.com/.
    Christiaan Venter, May 21, 2009
    #1
    1. Advertising

  2. Christiaan Venter

    7stud -- Guest

    Re: Can I use Hpricot to parse data into different array ele

    Christiaan Venter wrote:
    > Hi,
    >
    > I have a question about using Hpricot.
    >
    > If I do something like this
    >
    > doc = open("F:/search.html") { |f| Hpricot(f) }
    > doc.search(".price").each do |price|
    > prices = [price.inner_text]
    > puts prices[0]
    >
    > =>$21.95
    > $15.95
    >
    > Then it seems that my search results are in an array, but that both
    > prices are in a single element in the array because both come up when I
    > ask for the element at [0]
    > I'm a ruby newbie--what am I missing?
    > Thanks very much


    Not understanding loops? Better variable names?


    require 'rubygems'
    require 'hpricot'

    doc = open("html.txt") { |f| Hpricot(f) }

    doc.search(".price").each do |price_tag|
    price = price_tag.inner_text
    puts price
    end

    --output:--
    $21.95
    $15.95



    --
    Posted via http://www.ruby-forum.com/.
    7stud --, May 22, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    1
    Views:
    787
    Martin Honnen
    Jul 18, 2004
  2. insert an elem into a link list

    , Apr 4, 2006, in forum: C Programming
    Replies:
    4
    Views:
    309
    CBFalconer
    Apr 4, 2006
  3. Junkone
    Replies:
    1
    Views:
    100
    Mark Thomas
    Aug 12, 2008
  4. Replies:
    7
    Views:
    160
  5. Replies:
    3
    Views:
    285
    Sisyphus
    Jun 10, 2007
Loading...

Share This Page