Created
June 20, 2012 07:06
-
-
Save robdodson/2958538 to your computer and use it in GitHub Desktop.
Crawl a page full of links with Mechanize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mechanize' | |
# Create a new instance of Mechanize and grab our page | |
agent = Mechanize.new | |
page = agent.get('http://robdodson.me/blog/archives/') | |
# Find all the links on the page that are contained within | |
# h1 tags. | |
post_links = page.links.find_all { |l| l.attributes.parent.name == 'h1' } | |
# Click on one of our post links and store the response | |
post = post_links[1].click | |
doc = post.parser # Same as Nokogiri::HTML(page.body) | |
p doc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment