I'm trying to use a random key, value pair from a YAML file that looks like this:
'user_agents':
'Mozilla': '5.0 (compatible; Googlebot/2.1; +http://ift.tt/eSXNch)'
'Mozilla': '5.0 (compatible; Yahoo! Slurp; http://ift.tt/HakgT6)'
'Mozilla': '5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'
'Mozilla': '4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
Using this script:
require 'mechanize'
require 'yaml'
info = YAML.load_file('test-rand.yml')
@user_agent = info['user_agents'][info.keys.sample]
agent = Mechanize.new
agent.user_agent = @user_agent
if @user_agent.nil?
puts "The user agent is nil"
else
puts "Using: #{@user_agent}"
end
However while running this script I keep getting the The user agent is nil
, how do you pull a random key/value from a YAML file?
I've also tried @user_agent = info['user_agents'][info[rand(values.size)]]
Aucun commentaire:
Enregistrer un commentaire