私は、好きなイメージボードのスレッドからすべての画像やビデオをダウンロードするスクリプトを作成しようとしていました:2ch.hk
これらのファイルを非同期でダウンロードしたい)
は、ここでは、コードhttp://ideone.com/k2l4Hm
ファイルを非同期でダウンロードする
file = http.get(source).body
require 'net/http'
multithreading = false
Net::HTTP.start("2ch.hk", :use_ssl => true) do |http|
thread = http.get("/b/res/133467978.html").body
sources = []
thread.scan(/<a class="desktop" target="_blank" href=".+">.+<\/a>/).each do |a|
source = "/b#{/<a class="desktop" target="_blank" href="\.\.(.+)">.+<\/a>/.match(a).to_a[1]}"
sources << source
end
i = 0
start = Time.now
if multithreading
threads = []
sources.each do |source|
threads << Thread.new(i) do |j|
file = http.get(source).body #breaks everything
# type = /.+\.(.+)/.match(source)[1]
# open("#{j}.#{type}","wb") { |new_file|
# new_file.write(file)
# }
end
i += 1
end
threads.each do |thr|
thr.join
end
# until downloade=sources.size
#
# end
else
sources.each do |source|
file = http.get(source).body
type = /.+\.(.+)/.match(source)[1]
open("#{i}.#{type}","wb") { |new_file|
new_file.write(file)
}
i += 1
print "#{(((i).to_f/sources.size) * 100).round(2)}% "
end
puts
end
puts "Done. #{i} files were downloaded. It took #{Time.now - start} seconds"
end
私は、この行がすべてをクラッシュしたとしています。
file = http.get(source).body
これは問題ですか?
threads.each do |thr|
thr.join
end
エラーメッセージが不正なファイル記述子とするIOエラーから、常に異なっている「あなたは、Rubyインタプリタや拡張ライブラリのバグに遭遇したことがあります。」
私のコードを実行したい場合は、コードを実行するときに私のコードのものが削除される可能性があるので、4th行のスレッドへのリンクを2ch.hk/bの新しいスレッドに置き換えてください
rubyのバージョン:2.3.1、OS Xubuntu 16.10
:https://bugs.ruby-lang.org/issues/12661?next_issue_id=12660 –
は、あなたの代わりにそれにリンクのポストにあなたのコードを追加していただけますか? – deltaskelta