Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to use string variables in output files?

Reply
Thread Tools

how to use string variables in output files?

 
 
Pen Ttt
Guest
Posts: n/a
 
      03-30-2010
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/.

 
Reply With Quote
 
 
 
 
jbw
Guest
Posts: n/a
 
      03-30-2010
[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 <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Pen Ttt
Guest
Posts: n/a
 
      03-30-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
When to use automatic variables and when to use malloc Jorge Peixoto de Morais Neto C Programming 58 03-01-2007 02:53 PM
good practice to initalize all instance variables with type String to emptry string?? Matt Java 4 06-23-2004 06:14 AM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments