Rails 的旋转代理机械化?

问题描述 投票:0回答:1

有没有办法让 Mechanize 针对每个请求使用多个代理或轮换代理?

agent.set_proxy(addr, port, user = nil, pass = nil) 

设置代理,有没有办法可以将其设置为在每次请求期间更改?

ruby-on-rails proxy mechanize-ruby
1个回答
0
投票

只要您不打算对每个请求使用相同的代理,就可以。您可以开发一个 Helper 类,该类使用各种用户代理字符串的简单随机生成器以及代理提供的主机。

class ProxyMeshHelper

  def initialize
  end

  def ex
    return {
      host: host_randomizer,
      port: port,
      proxy_user: proxy_user,
      proxy_pass: proxy_pass,
      user_agent_alias: user_agent_randomizer
    }
  end

  def host_randomizer
    host_arr.sample
  end

  def host_arr
    ['us-tx.proxy.com', 'us-nc.proxy.com'...etc]
  end

  def user_agent_randomizer
    user_agents.sample
  end

  def user_agents
    ["Linux Firefox", "Linux Konqueror", "Linux Mozilla", "Mac Firefox", "Mac Mozilla", "Mac Safari 4", "Mac Safari", "Windows Chrome", "Windows Edge", "Windows Mozilla", "Windows Firefox", "iPhone", "Android"]
  end

  def port
    ENV['proxy_port_number']
  end

  def proxy_user
    ENV['proxy_mesh_username']
  end

  def proxy_pass
    ENV['proxy_mesh_password']
  end

  def proxy_mesh_mechanize_template
    proxy = AppMaps::ProxyMeshHelper.new.ex
    agent = Mechanize.new { |agent|
      agent.user_agent_alias = proxy[:user_agent_alias]
    }
    agent.follow_meta_refresh = true
    agent.set_proxy(proxy[:host], proxy[:port], proxy[:proxy_user], proxy[:proxy_pass])
    agent
  end

end
© www.soinside.com 2019 - 2024. All rights reserved.