使用 YAML 文件中的随机 key/value
Using random key/value from YAML file
我正在尝试使用 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 文件中随机提取 key/value?
我也试过了@user_agent = info['user_agents'][info[rand(values.size)]]
How do you pull a random key/value from a YAML file?
您当前的 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....
我想我找到了一个解决方案,如果有人有更好的解决方案请告诉我,我将 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)"}
我正在尝试使用 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 文件中随机提取 key/value?
我也试过了@user_agent = info['user_agents'][info[rand(values.size)]]
How do you pull a random key/value from a YAML file?
您当前的 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....
我想我找到了一个解决方案,如果有人有更好的解决方案请告诉我,我将 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)"}