Get a web page with open-uri

A

Andy P.

Hello !

I'm trying to get a web page width open-uri width this code :

require 'open-uri'
open("http://www.google.com") {|src|
open("test.htm","wb") {|dst|
dst.write(src.read)
}
}

But it returns me :
cannot convert File into String

src should be a Tempfile.
I think there is an other method called open, what is the full name of
the open uri method ? How could I do ?

Thanks.
 
A

Andy P.

Glenn said:
You're looking for the class method in File:

File.open("test.htm","wb") {|dst|


Yes but it don't work with :
open("www.google.com") {|src|

I'm not trying that in irb, I imported all the files of uri, open-uri,
but it don't work. What is the full name of the open uri method who
creates a TempFile ?
Must I import other files ?

Thanks.
 
R

Robert Klemme

2009/7/8 Andy P. said:
Yes but it don't work with :
open("www.google.com") {|src|

I'm not trying that in irb, I imported all the files of uri, open-uri,
but it don't work. What is the full name of the open uri method who
creates a TempFile ?
Must I import other files ?

Can you show specific code and errors? As for Andrew, this worked for
me out of the box:

09:26:33 Temp$ irb19
Ruby version 1.9.1
irb(main):001:0> require 'open-uri'
=3D> true
irb(main):002:0> open("http://www.google.com") {|src|
irb(main):003:1* open("test.htm","wb") {|dst|
irb(main):004:2* dst.write(src.read)
irb(main):005:2> }
irb(main):006:1> }
=3D> 5337
irb(main):007:0> exit
09:26:54 Temp$ wc test.htm
9 190 5337 test.htm
09:27:02 Temp$

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
A

Andy P.

Robert said:
Can you show specific code and errors? As for Andrew, this worked for
me out of the box:

09:26:33 Temp$ irb19
Ruby version 1.9.1
irb(main):001:0> require 'open-uri'
=> true
irb(main):002:0> open("http://www.google.com") {|src|
irb(main):003:1* open("test.htm","wb") {|dst|
irb(main):004:2* dst.write(src.read)
irb(main):005:2> }
irb(main):006:1> }
=> 5337
irb(main):007:0> exit
09:26:54 Temp$ wc test.htm
9 190 5337 test.htm
09:27:02 Temp$

Kind regards

robert


Sorry for the delay.

Yes, it works !


Now, I would want to download an image file using sockets.

require 'socket'
host = "www.google.fr"
port = 80
path = "/intl/fr_fr/images/logo.gif" #google logo
request = "GET #{path} GIF\r\n\r\n"
socket = TCPSocket.open(host, port)
socket.print(request)
response = socket.read
img = response.split("\r\n\r\n", 2)
puts img

It doesn't works : it returns :
"HTTP/1.1 400 Bad Request"

How to do ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top