繁体   English   中英

Open-uri throw error =>(URI :: InvalidURIError)

[英]Open-uri throwing error => (URI::InvalidURIError)

我有一个程序,我正在用于测试目的我正在做的是抓取Web开放代理,并记录它们的信息,但这是一种非常不同类型的代理刮刀,因为它创建了一堆随机代理在执行之前在文件内部之前例如:

def create_possibles
  puts "Creating random possible proxies..".green.bold
  1.times do 
    port = rand(2000..8080)
    1.times do 
      ip = Array.new(4){rand(256)}.join('.')
      possible_proxy = "#{ip}:#{port}"
      File.open("possible_proxies.txt", "a") {|s| s.puts(possible_proxy)}
    end
  end
end
#<= 189.96.49.87:7990

我想用“可能的代理”打开它并查看它是否有效,但是当我使用下面的代码时它只会抛出该错误:

def check_possibles
  IO.read("possible_proxies.txt").each_line do |proxy|
    puts open('http://google.com', :proxy => "http://#{proxy}")
  end
end

我有两个问题:

  1. 这是否意味着代理无效,如果有,是否有办法跳过文件中的行? 可能通过使用nextskip
  2. 如果这并不意味着代理无效,那么它是什么意思,我在我的代码中做错了什么,它正在读取url的错误?

完整错误:

C:/Ruby22/lib/ruby/2.2.0/uri/rfc3986_parser.rb:66:in `split': bad URI(is not URI
?): http://189.96.49.87:7990 (URI::InvalidURIError)

编辑:

有人告诉我尝试URI.parse ,我得到同样的错误:

C:/Ruby22/lib/ruby/2.2.0/uri/rfc3986_parser.rb:66:in `split': bad URI(is not URI
?): http://195.239.61.210:4365 (URI::InvalidURIError) #<= Different IP

当您使用#each_line迭代ruby中的#each_line ,它会为您提供包括换行符在内的每一 Ruby的URI lib不喜欢换行符。 只需更换

:proxy => "http://#{proxy}"

:proxy => "http://#{proxy.chomp}"

String#chomp将切断字符串末尾的任何换行符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM