前幾天看了個(gè)抓取斗魚(yú)彈幕的帖子,覺(jué)得挺有意思的一睁。本來(lái)也想學(xué)著做下钻弄,發(fā)現(xiàn)也不是那么好弄佃却,現(xiàn)在就做了一個(gè)抓取英雄聯(lián)盟頁(yè)面的主播圖片的實(shí)驗(yàn)秸歧,只是抓一個(gè)頁(yè)面的巍耗。
require 'net/http'
require 'open-uri'
def query_url(url)
return Net::HTTP.get(URI.parse(url))
end
def save_url(url,dir,filename)
filename = url[39,70] if filename == nil || filename.empty?
open(url) do |f| if true
File.new("#{dir}#{filename}","wb").close
open("#{dir}#{filename}","wb") do |fo|
while buf = f.read(1024) do
fo.write buf
STDOUT.flush
end
end
end
end
end
begin
start_url = 'http://www.douyutv.com/directory/game/LOL'
while start_url != nil && !start_url.empty? do
print "開(kāi)始下載#{start_url}\n"
content = query_url(start_url)
imgs = content.scan(/http://rpic.douyucdn.cn/z1603/13/\w{2}/\w{5,10}_\w{12}.jpg/)
for img in imgs
url = img
save_url(url,File.dirname(FILE),nil)
end
break;
end
end