Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a local HTML file with Mechanize

Tags:

ruby

mechanize

I am building a crawler, I know how to use ruby mechanize to read a page from the net using this code:

require 'mechanize'
agent = Mechanize.new
agent.get "http://google.com"

But can I use Mechanize to read an HTML file from the file system? How?

like image 318
c2h2 Avatar asked Sep 28 '11 16:09

c2h2


1 Answers

just using the file:// protocol worked great for me:

html_dir = File.dirname(__FILE__)
page = agent.get("file:///#{html_dir}/example-file.html")

and about the raised question why someone would use mechanize to read local html files: I found it necessary for testing purposes - just store an example file locally and run your rspec against it.

like image 58
bento Avatar answered Sep 21 '22 14:09

bento