-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
37 lines (32 loc) · 967 Bytes
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require 'nokogiri'
require 'net/http'
require 'securerandom'
url = 'https://free-proxy-list.net/'
uri = URI(url)
headers = {
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
begin
req = Net::HTTP::Get.new(uri, headers)
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
http.request(req)
}
doc = Nokogiri::HTML(response.body)
proxies = []
doc.css('tbody tr').each do |row|
tds = row.css('td')
if tds.size >= 2
ip = tds[0].text
port = tds[1].text
proxies << "#{ip}:#{port}"
end
end
if proxies.empty?
puts "No proxies found."
else
random_proxy = proxies[SecureRandom.random_number(proxies.size)]
puts "Random proxy: #{random_proxy}"
end
rescue StandardError => e
puts "Error: #{e.message}"
end