Problem fetching web-page

J

James Mulholland

I'm not sure if this is the best place to ask this, but I hope someone
will be able to help, or at least point me somewhere else.

I've written a screen-scrapper (in Perl) for digg.com. It uses
HTTP::Lite to retrieve the page and regexp's to parse information. It
works, but I'd like to create a Ruby version to help me learn Ruby.

Here is the code I'm trying to use:

require 'net/http'
require 'uri'

Net::HTTP.start( 'www.digg.com', 80 ) do |http|
print( http.get( '/' ).body )
end

If I use this to get another site (eg slashdot.org) it returns all the
HTML, as expected. With digg.com, I get this:

<BR clear=3D"all">
<HR noshade size=3D"1px">
<ADDRESS>
Generated Mon, 28 Nov 2005 20:22:05 GMT by Prolexic.com (SI2LON1/2.0)
</ADDRESS>
</BODY></HTML>

That looks like (I'm guessing) some kind of return message from a
load-balancer or other proxy. I've tried this from 3 different systems
(which use different ISPs) so I don't think it's my system.

Does anyone have any ideas about this? Why does the Perl code work,
but not the Ruby? Is there a fix?

Using Ruby 1.8.3 under Linux, also tried it with Ruby 1.8.2 on Mac OS X.

TIA
 
G

Gregory Brown

That looks like (I'm guessing) some kind of return message from a
load-balancer or other proxy. I've tried this from 3 different systems
(which use different ISPs) so I don't think it's my system.

Using open-uri, this is what I get:

irb(main):001:0> require 'open-uri'
=3D> true
irb(main):002:0> open('http://www.digg.com').read
OpenURI::HTTPError: 403 Forbidden
from /usr/local/lib/ruby/1.8/open-uri.rb:574:in `proxy_open'
from /usr/local/lib/ruby/1.8/open-uri.rb:525:in `direct_open'
from /usr/local/lib/ruby/1.8/open-uri.rb:169:in `open_loop'
from /usr/local/lib/ruby/1.8/open-uri.rb:164:in `catch'
from /usr/local/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
from /usr/local/lib/ruby/1.8/open-uri.rb:134:in `open_uri'
from /usr/local/lib/ruby/1.8/open-uri.rb:424:in `open'
from /usr/local/lib/ruby/1.8/open-uri.rb:85:in `open'
from (irb):2
 
J

James Britt

Gregory said:
Using open-uri, this is what I get:


When I launch the Web 2.0 Validator (web2.0validator.com), I pointed it
at digg.com (among other sites) and it rejected the request.

I changed the user agent sent in the request headers, and the requests
were fine after that.


James Britt

--

http://www.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 
J

James Mulholland

I changed the user agent sent in the request headers, and the requests
were fine after that.

Precisely that -- thanks! Seems like changing the UA to anything (I
chose the well-known "foobar" browser for my first test :) will do the
trick:

print( http.get( '/', "User-Agent" =3D> "foobar" ).body )

Thanks again.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top