website screen scraping with Mechanize or Rubyful Soup

D

Dan Kohn

I'm trying to get some website screen scraping working, but I'm
suffering from a lack of examples and documentation for either
WWW::Mechanize or Rubyful Soup.

With WWW::Mechanize, the only example I found was
http://www.zenspider.com/pipermail/ruby/2005-July/002068.html. I tried
to simplify this to the script below, but it just prints out "My wife
is ".

Rubyful Soup <http://www.crummy.com/software/RubyfulSoup/> also seems
like a great library, but there doesn't seem to be a single example
(only Python ones
<http://www.crummy.com/software/BeautifulSoup/examples.html>).

#!/usr/bin/env ruby

require 'mechanize'

agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE 6'

# get first page
page = agent.get('http://www.dankohn.com/')
md = page.body.match /My wife, (\w+\s\w+)<\/a>/m

printf "My wife is ", md




Thanks in advance for any help you can offer.
 
L

Lyndon Samson

------=_Part_8218_13399008.1126508342542
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

No quite what you wanted but

This can turn HTML in well formed XML

http://rubyforge.org/projects/tidy/

which is much easier to parse.

Maybe worth a look.

------=_Part_8218_13399008.1126508342542--
 
J

James Britt

Dan said:
I'm trying to get some website screen scraping working, but I'm
suffering from a lack of examples and documentation for either
WWW::Mechanize or Rubyful Soup.

With WWW::Mechanize, the only example I found was
http://www.zenspider.com/pipermail/ruby/2005-July/002068.html. I tried
to simplify this to the script below, but it just prints out "My wife
is ".

What are you actually trying to accomplish?

James




--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
 
D

Dan Kohn

My ultimate goal is to create a series of screen scrapers that are able
to access airline websites (including entering username and password,
dealing with redirects, etc.), find my mileage and recent flights,
parse the data, put it in some variables, and save it to MySQL (with
rails).

I was trying to start with baby steps to understand the methods these
libraries support. Specifically, I was trying to fetch my own web
page, and then use a regex to match to my wife's name, "Julie Pullen",
since I have link text on www.dankohn.com saying "My wife, Julie
Pullen". I was then going to gradually increase the complexity of the
scraping.

Thanks in advance for any example scripts or documentation that you can
provide showing web scraping in ruby.

And Lyndon, I'm a huge fan of Tidy for cleaning up my own web pages,
but I'm not sure it's helpful here, as was aiming to use regexes to
parse the HTML rather than the DOM.
 
M

Michel Martens

Hi

I'm trying to get some website screen scraping working, but I'm
suffering from a lack of examples and documentation for either
WWW::Mechanize or Rubyful Soup.
=20
With WWW::Mechanize, the only example I found was
http://www.zenspider.com/pipermail/ruby/2005-July/002068.html. I tried
to simplify this to the script below, but it just prints out "My wife
is ".
=20
Rubyful Soup <http://www.crummy.com/software/RubyfulSoup/> also seems
like a great library, but there doesn't seem to be a single example
(only Python ones
<http://www.crummy.com/software/BeautifulSoup/examples.html>).
=20
#!/usr/bin/env ruby
=20
require 'mechanize'
=20
agent =3D WWW::Mechanize.new
agent.user_agent_alias =3D 'Windows IE 6'
=20
# get first page
page =3D agent.get('http://www.dankohn.com/')
md =3D page.body.match /My wife, (\w+\s\w+)<\/a>/m
=20
printf "My wife is ", md

print "My wife is ", md[1]



Michel.
 
L

Lyndon Samson

------=_Part_11287_19310825.1126568412932
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
=20
=20
And Lyndon, I'm a huge fan of Tidy for cleaning up my own web pages,
but I'm not sure it's helpful here, as was aiming to use regexes to
parse the HTML rather than the DOM.



Well, DOM allows you to use XPath, which is a powerfull query mechanism.

This=20
http://www-128.ibm.com/developerworks/java/library/j-jtp03225.html?ca=3Ddgr=
-jw26XQueryis
XQuery specific, but relies
on XPath.

And example from the article

//td[contains(a/small/text(), "New York, NY")]

------=_Part_11287_19310825.1126568412932--
 
D

dave.burt

If you can use a Windows box, Watir might be the best-fit.

Install (using Gem or the Windows setup program that is available at
http://wtr.rubyforge.org/) and IRB this:

require 'watir'
ie = Watir::IE.new
ie.goto "dankohn.com"
julie_lines = ie.text.scan(/.*Julie.*/)
link = ie.link:)text, /Julie/)
link.click


Cheers,
Dave
 
D

Dan Kohn

Looks cool, thanks. I'm developing on a Windows machine, but plan to
move to a "real" machine for production. So it looks like Mechanize,
REXML, and XQuery will be the best bet.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top