
03.07.2009, 15:33
|
|
Познающий
Регистрация: 14.01.2009
Сообщений: 93
Провел на форуме: 244235
Репутация:
39
|
|
Многопоточный парсер беков из яхи
Код:
puts Time.now
def urlescape(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end.tr(' ', '+')
end
require 'net/http'
numthreads = 10
input = File.new('input.txt', 'r')
output = File.new('output.txt', 'w')
threads = Array.new
1.upto(numthreads) do |w|
threads << Thread.new(w) do |w|
while line = input.gets
next if line =~ /^#/
yurl = "/export?p=#{urlescape(line.chomp)}&bwm=i&fr=sfp"
line = Net::HTTP.get('siteexplorer.search.yahoo.com', yurl)
line.scan(/\t(.+?)\t/) do |url|
output.write(url[0]+"\n") unless url[0].index('http').nil?
end
end
end
end
threads.each do |t| t.join end
input.close
output.close
puts Time.now
|
|
|