csv - Ruby Mechanize form input field text -
resolved - "abc = list.scan(/[([^)]+)]/).last.first" line correct included quotes, website search form did not accept. corrected abc = list.scan(/\"([^)]+)\"/).join.
thanks help.
i have automate search using list of 100 keywords in csv file.
with mechanize, can submit search using example (http://mechanize.rubyforge.org/guide_rdoc.html):
agent = mechanize.new page = agent.get('http://google.com/') google_form = page.form('f') google_form.q = 'ruby mechanize' page = agent.submit(google_form) pp page however, when make loop through csv file, returns error (in example, first csv entry 'ruby mechanize':
#i have imported csv list, looping through array "raw_list" raw_list.each |list| abc = list.scan(/\[([^\)]+)\]/).last.first # tested "puts abc" returned "ruby mechanize", don't understand why rest of doesn't work agent = mechanize.new page = agent.get('http://google.com/') google_form = page.form('f') google_form.q = abc #even though abc = "ruby mechanize", error occurs. page = agent.submit(google_form) pp page it doesn't seem take variable "abc", works if manually type in 'ruby mechanize' though both same.
the error appears is:
c:filename: in `block (2 levels) in <top (required)>': undefined method `text' nil:nilclass (nomethoderror) c:/railsinstaller/ruby2.0.0/lib/ruby/gems/2.0.0/gems/mechanize-2.7.3/lib/mechanize.rb:442:in `get' c:/users/victor/rubymineprojects/untitled/scraper.rb:23:in `block in <top (required)>' c:/users/victor/rubymineprojects/untitled/scraper.rb:19:in `each' c:/users/victor/rubymineprojects/untitled/scraper.rb:19:in `<top (required)>' -e:1:in `load' -e:1:in `<main>' any appreciated.
your error telling on line 19 in code causing issue line 442 in mechanize.
i tried sample out in irb , seems work fine:
2.2.2 :001 > require 'mechanize' => true 2.2.2 :002 > agent = mechanize.new => #<mechanize:... 2.2.2 :003 > page = agent.get('http://google.com/') => #<mechanize::page ... 2.2.2 :004 > google_form = page.form('f') => #<mechanize::form ... 2.2.2 :005 > google_form.q => "" 2.2.2 :006 > abc = "ruby mechanize" => "ruby mechanize" 2.2.2 :007 > google_form.q = abc => "ruby mechanize" 2.2.2 :008 > page = agent.submit(google_form) => #<mechanize::page ... scan return nil if nothing found error happening here:
abc = list.scan(/\[([^\)]+)\]/).last.first http://ruby-doc.org/stdlib-2.2.0/libdoc/strscan/rdoc/stringscanner.html
you can replace with:
abc = list.scan(/\[([^\)]+)\]/).join you'll string although may "".
Comments
Post a Comment