how to use string variables in output files?

Discussion in 'Ruby' started by Pen Ttt, Mar 30, 2010.

  1. Pen Ttt

    Pen Ttt Guest

    hi,i want to prase the web
    'http://quotes.money.163.com/corp/1034/code=601398.html'
    there are 27 tables in the web,
    i made a for loop to get every table in the web,
    /home/pt/test/table0 is the output of tables[0],
    /home/pt/test/table1 is the output of tables[1],
    ,,,,,
    /home/pt/test/table26 is the output of tables[27],


    here is my programme,

    require 'hpricot'
    require 'open-uri'
    require 'iconv'
    data =
    open('http://quotes.money.163.com/corp/1034/code=601398.html').read
    html = Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",data)
    u8doc = Hpricot(html[0])
    tables = u8doc.search("table")
    i=tables.size
    puts i
    for j in 0..i-1
    File.open('/home/pt/test/table'+j.to_s+'.html', 'w') {|f|
    f.write('tables['+j.to_s+']'.inner_html) }
    end

    it can't work,can you revise it for me?
    think you
    --
    Posted via http://www.ruby-forum.com/.
     
    Pen Ttt, Mar 30, 2010
    #1
    1. Advertising

  2. Pen Ttt

    jbw Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi,

    do you want something like this?:

    require 'hpricot'
    require 'open-uri'
    require 'iconv'


    data = open('http://quotes.money.163.com/corp/1034/code=601398.html').read

    html = Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",data)

    u8doc = Hpricot(html[0])

    tables = u8doc.search("table")

    i=tables.size

    puts i

    for j in 0...i do

    output = File.new("/Users/jbw/table" + j.to_s + ".html", "w")
    output.puts tables[j]
    output.close

    end


    On Tue, Mar 30, 2010 at 1:58 AM, Pen Ttt <> wrote:
    > hi,i want to prase the web
    > 'http://quotes.money.163.com/corp/1034/code=601398.html'
    > there are 27 tables in the web,
    > i made a for loop to get every table in the web,
    > /home/pt/test/table0 is the output of tables[0],
    > /home/pt/test/table1 is the output of tables[1],
    > ,,,,,
    > /home/pt/test/table26 is the output of tables[27],
    >
    >
    > here is my programme,
    >
    > require 'hpricot'
    > require 'open-uri'
    > require 'iconv'
    > data =
    > open('http://quotes.money.163.com/corp/1034/code=601398.html').read
    > html = Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",data)
    > u8doc = Hpricot(html[0])
    > tables = u8doc.search("table")
    > i=tables.size
    > puts i
    > for j in 0..i-1
    > File.open('/home/pt/test/table'+j.to_s+'.html', 'w') {|f|
    > f.write('tables['+j.to_s+']'.inner_html) }
    > end
    >
    > it can't work,can you revise it for me?
    > think you
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --
    jbw
     
    jbw, Mar 30, 2010
    #2
    1. Advertising

  3. Pen Ttt

    Pen Ttt Guest

    think for your help
    the right code is :
    require 'hpricot'
    require 'open-uri'
    require 'iconv'
    data
    =open('http://quotes.money.163.com/corp/1034/code=601398.html').read
    html = Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE",data)
    u8doc = Hpricot(html[0])
    tables = u8doc.search("table")
    i=tables.size
    for j in 0..i-1
    output = File.new("/home/pt/test/table" + j.to_s + ".html", "w")
    output.puts tables[j].inner_html
    output.close
    end
    i still have some problems:
    1\when i got the table's html,for example tables[2],
    can i get row size or column size of the tables[2].
    2\can i use array to load the data in the tables[2]??

    --
    Posted via http://www.ruby-forum.com/.
     
    Pen Ttt, Mar 30, 2010
    #3
    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. Matt
    Replies:
    4
    Views:
    1,416
    Roedy Green
    Jun 23, 2004
  2. Matt
    Replies:
    3
    Views:
    541
    Tor Iver Wilhelmsen
    Sep 17, 2004
  3. Jorge Peixoto de Morais Neto

    When to use automatic variables and when to use malloc

    Jorge Peixoto de Morais Neto, Feb 20, 2007, in forum: C Programming
    Replies:
    58
    Views:
    1,356
    Eric Sosman
    Mar 1, 2007
  4. Replies:
    9
    Views:
    997
  5. Scott Bass
    Replies:
    4
    Views:
    164
    Big and Blue
    Mar 21, 2005
Loading...

Share This Page