使用YAML文件中的随机键/值

时间:2016-04-19 18:34:05

标签: ruby random yaml key-value

我正在尝试使用YAML文件中的随机键值对,如下所示:

'user_agents':
  'Mozilla': '5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
  'Mozilla': '5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)'
  '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)'

使用此脚本:

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

然而,在运行此脚本时,我不断获取The user agent is nil,如何从YAML文件中提取随机键/值?

我也试过@user_agent = info['user_agents'][info[rand(values.size)]]

2 个答案:

答案 0 :(得分:2)

我想我找到了一个解决方案,如果有人有更好的解决方案请告诉我,我将YAML文件更改为只有一个Mozilla具有多个值:

YAML:

'user_agents':
  'Mozilla': ['5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', '4.0 (compatible; MSIE 6.0; Windows NT 5.1)', '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', '5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)'] 
然后我接受了键和值,并使用以下方法将YAML文件分解为键值对:

info = YAML.load_file('test-rand.yml')
info['user_agents'].each do |k,v|

从那里我将值转换为数组,对数组进行采样,并将结果保存到变量中,然后我创建了一个名为@user_agent的新哈希,并为其提供了k的密钥和价值arr_val.to_s

arr_val = v.to_a.sample
@user_agent = {k => arr_val.to_s}

完整脚本:

require 'mechanize'
require 'yaml'

info = YAML.load_file('test-rand.yml')
info['user_agents'].each do |k,v|
  arr_val = v.to_a.sample
  @user_agent = {k => arr_val.to_s}
end

agent = Mechanize.new
agent.user_agent = @user_agent
if @user_agent.nil?
  puts "The user agent is nil"
else
  puts "Using: #{@user_agent}"
end
#<= Using: {"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"}
#<= Using: {"Mozilla"=>"5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"}
#<= Using: {"Mozilla"=>"4.0 (compatible; MSIE 6.0; Windows NT 5.1)"}

答案 1 :(得分:1)

  

如何从YAML文件中提取随机键/值?

您当前的yaml文件包含相同的键Mozilla。 ruby将yaml文件转换为哈希值。在ruby哈希中只能包含唯一键。所以你的yaml文件看起来像:

=> {"user_agents"=>{"Mozilla"=>"4.0 (compatible; MSIE 6.0; Windows NT 5.1)"}}

带有唯一键的yml文件:

'user_agents':
  'Mozilla_1': '5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
  'Mozilla_2': '5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)'
  'Mozilla_3': '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': '4.0 (compatible; MSIE 6.0; Windows NT 5.1)'

Rails控制台:

=> info = YAML.load_file('y.yml')
=> {"user_agents"=>
  {"Mozilla_1"=>"5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
   "Mozilla_2"=>"5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)",
   "Mozilla_3"=>"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"=>"4.0 (compatible; MSIE 6.0; Windows NT 5.1)"}}

获取随机而非关键字:

=> info["user_agents"]["Mozilla_#{rand(1..4)}"]
#> "5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
=>  info["user_agents"]["Mozilla_#{rand(1..4)}"]
#> "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"
=> info["user_agents"]["Mozilla_#{rand(1..4)}"]
#> "5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)"
=> and so on....