0

No clue on this one.. Its acting weird, and the response I want is getting put to the terminal instead of the file.. But I never asked the results to be put to the terminal. Odd.. How can I get the results from Mechanize / Nokogiri printed to a file?

a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}

a.set_proxy '64.213.148.50', 8080
a.get('http://www.amazon.com')

a.page.forms[0]["field-keywords"] = "B009NWFP5Q"

a.page.forms[0].submit

try = a.page.link_with(:text => "Madden Girl Women's Harmonee Ballet Flat").click

url = a.get(try).body

doc = Nokogiri::HTML(open(url))

result =  doc.inspect

after = "after.txt"

File.write(after, result)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ToddT
  • 3,084
  • 4
  • 39
  • 83
  • Well.. I figured it out.. that doc.inspect is somehow sending the results to the terminal.. not sure why, but I took that out and wrote it to a file instead, and everything is Kosher now.. all good. – ToddT Jun 06 '15 at 13:31
  • `inspect` is not designed for outputting to a file. It's a debugging tool and its output is prone to change. Instead, create a `to_s` method for the object that makes sense for your use, and rely on it for your output. In Nokogiri's case, use `to_html` or `to_xml` to re-render the DOM to a standard output. – the Tin Man Jun 11 '15 at 23:13

1 Answers1

0

As said by the Tin man, inspect is a debugging tool, so it writes to STDERR. In a Unix environment you can redirect the error logging to a file in the following way:

Overwriting output file:

$ ruby script.rb 2> output_file

Appends the STDERR data to th eoutput file:

$ ruby script.rb 2>> output_file
RBrandao
  • 102
  • 1
  • 13