Get a web page with open-uri

View: New views
8 Messages — Rating Filter:   Alert me  

Get a web page with open-uri

by Andy P. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.
--
Posted via http://www.ruby-forum.com/.


Re: Get a web page with open-uri

by Andrew Timberlake-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 8, 2009 at 11:52 AM, Andy P.<andycootlapin@...> wrote:
> require 'open-uri'
> open("http://www.google.com") {|src|
>  open("test.htm","wb") {|dst|
>      dst.write(src.read)
>  }
> }

Andy

Pasted the code directly into irb and it worked perfectly.

Andrew Timberlake
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings


Re: Get a web page with open-uri

by Glenn Jackman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At 2009-07-08 05:52AM, "Andy P." wrote:
>  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|

You're looking for the class method in File:

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

>        dst.write(src.read)
>    }
>  }


--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous


Re: Get a web page with open-uri

by Andy P. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Glenn Jackman wrote:

> At 2009-07-08 05:52AM, "Andy P." wrote:
>>  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|
>
> 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.
--
Posted via http://www.ruby-forum.com/.


Re: Get a web page with open-uri

by Robert Klemme-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/8 Andy P. <andycootlapin@...>:

> Glenn Jackman wrote:
>> At 2009-07-08 05:52AM, "Andy P." wrote:
>>>  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|
>>
>> 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 ?

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

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


Re: Get a web page with open-uri

by Andy P. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Robert Klemme wrote:

> 2009/7/8 Andy P. <andycootlapin@...>:
>>> You're looking for the class method in File:
>> 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'
> => 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 ?
--
Posted via http://www.ruby-forum.com/.


Re: Get a web page with open-uri

by 7stud ---2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> request = "GET #{path} GIF\r\n\r\n"
>
> It doesn't works : it returns :
> "HTTP/1.1 400 Bad Request"
>
> How to do ?

http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
--
Posted via http://www.ruby-forum.com/.


Re: Get a web page with open-uri

by Andy P. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

7stud -- wrote:
>> request = "GET #{path} GIF\r\n\r\n"
>>
>> It doesn't works : it returns :
>> "HTTP/1.1 400 Bad Request"
>>
>> How to do ?
>
> http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

Yes it works, thanks all !
--
Posted via http://www.ruby-forum.com/.