NeoVe NeoVe - 29 days ago 8
Ruby Question

How to scrape website with search button

I have this website: codigos if You look at it, it has a selection field at left, and a go button at right, I need to scrape some of the items on left.

But, how can I tell to mechanize in ruby how to access that selection field and then make the search and scrape it?

I've seen examples with login forms but I don't know if it can really suit this case though.

Answer

The <select> tag is contained within a <form> tag, so you need to locate the form and then you can set the option by passing the name of the select list and specifying the appropriate option:

require 'mechanize'

mechanize = Mechanize.new
page = mechanize.get('http://comext.aduana.cl:7001/codigos/')

form = page.forms.first
form["wlw-select_key:{actionForm.opcion}"] = "Aduana"

result_page = form.submit
result_page.uri #=> http://comext.aduana.cl:7001/codigos/buscar.do;jsessionid=2hGwYfzD76WKGfFbXJvmS2yq4K19VnZycJfH8hJMTzRFhln4pTy2!1794372623!-1405983655!8080!-1
Comments