How to set custom user-agent for Mechanize in Rails
You can set the user agent from an alias
a = Mechanize.new
a.user_agent_alias = 'Mac Safari'
Available aliases are stored in the AGENT_ALIASES
constant.
p Mechanize::AGENT_ALIASES
Otherwise, use #user_agent
to set your custom user agent.
a = Mechanize.new
a.user_agent = 'Custom agent'
Setting a HTTP user agent with mechanize?
Turns out that the issue was that user_agent_alias
requires a specific type. All acceptable types are as follows:
- Linux Firefox (3.6.1)
- Linux Konqueror (3)
- Linux Mozilla
- Mac Firefox (3.6)
- Mac Mozilla
- Mac Safari (5)
- Mac Safari 4
- Mechanize (default)
- Windows IE 6
- Windows IE 7
- Windows IE 8
- Windows IE 9
- Windows Mozilla
- iPhone (3.0)
- iPad
- Android (Motorola Xoom)
Working code:
require 'rubygems'
require 'mechanize'
m = Mechanize.new
m.user_agent_alias = 'Mac Safari 4'
page = m.get("http://whatsmyuseragent.com/")
html = Nokogiri::HTML(page.body)
puts html.xpath('//*[(@id = "body_lbUserAgent")]').map(&:content)
Ruby Mechanize: user agents?
Yes. Look at https://github.com/sparklemotion/mechanize/blob/master/lib/mechanize.rb#L115:
AGENT_ALIASES = {
'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Windows IE 7' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Windows Mozilla' => 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6',
'Mac Safari' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10',
'Mac FireFox' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6',
'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401',
'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624',
'Linux Firefox' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.1) Gecko/20100122 firefox/3.6.1',
'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)',
'iPhone' => 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3',
'Mechanize' => "WWW-Mechanize/#{VERSION} (http://rubyforge.org/projects/mechanize/)"
}
POST request capture with Mechanize
The post method expects the parameters as a hash. Try:
agent.post("http://www.etr.gov.ar/getSmsResponse.php", {"parada" => "4152", "linea"=>"112"})
Cannot sign in using form with Mechanize
The server also checks the presence of the submit button parameter 'send'.
Add this line before the form.submit
form.add_field! 'send','Submit'
Related Topics
How to Rescue Timeout Issues (Ruby, Rails)
Dynamically Create Index with Mongoid
"Autotest/Rails [...] Doesn't [...] Exist. Aborting"
Installing Native Ruby Extensions on Windows for Jekyll
Garbage Collector Tuning in Ruby 1.9
Setting Environment Variables with Puppet
How to Use Rails 5.1.0 and Jquery
Ruby Operator Overloading Question
Rails Change Routing of Submit in Form_For
Ruby Rest-Client File Upload as Multipart Form Data with Basic Authenticaion
Attempting to Install Libv8, "Failed to Build Gem Native Extension"
How to Get Words Frequency in Efficient Way with Ruby
How to Sort a Ruby Hash Alphabetically by Keys
Ruby: Remove Whitespace Chars at the Beginning of a String
Active Record with Delegate and Conditions