Ruby:多線(xiàn)程下載博客文章到本地的完整代碼
復(fù)制代碼 代碼如下:
#encoding:utf-8
require 'net/http'
require 'thread'
require 'open-uri'
require 'nokogiri'
require 'date'
$queue = Queue.new
#文章列表頁(yè)數(shù)
page_nums = 8
page_nums.times do |num|
$queue.push("http://www.cnblogs.com/hongfei/default.html?page="+num.to_s)
end
threads = []
#獲取網(wǎng)頁(yè)源碼
def get_html(url)
html = ""
open(url) do |f|
html = f.read
end
return html
end
def fetch_links(html)
doc = Nokogiri::HTML(html)
#提取文章鏈接
doc.xpath('//div[@class="postTitle"]/a').each do |link|
href = link['href'].to_s
if href.include?"html"
#add work to the queue
$queue.push(link['href'])
end
end
end
def save_to(save_to,content)
f = File.new("./"+save_to+".html","w+")
f.write(content)
f.close()
end
#程序開(kāi)始的時(shí)間
$total_time_begin = Time.now.to_i
#開(kāi)辟的線(xiàn)程數(shù)
threadNums = 10
threadNums.times do
threadsThread.new do
until $queue.empty?
url = $queue.pop(true) rescue nil
html = get_html(url)
fetch_links(html)
if !url.include?"?page"
title = Nokogiri::HTML(html).css('title').text
puts "["+ Time.now.strftime("%H:%M:%S") + "]「" + title + "」" + url
save_to("pages/" + title.gsub(/\//,""),html) if url.include?".html"
end
end
end
end
threads.each{|t| t.join}
#程序結(jié)束的時(shí)間
$total_time_end = Time.now.to_i
puts "線(xiàn)程數(shù):" + threadNums.to_s
puts "執(zhí)行時(shí)間:" + ($total_time_end - $total_time_begin).to_s + "秒"
多線(xiàn)程部分講解
復(fù)制代碼 代碼如下:
$queue = Queue.new
#文章列表頁(yè)數(shù)
page_nums = 8
page_nums.times do |num|
$queue.push("http://www.cnblogs.com/hongfei/default.html?page="+num.to_s)
end
首先聲明一個(gè)Queue隊(duì)列,然后往隊(duì)列中添加文章列表頁(yè),以便后面可以從這些列表頁(yè)中提取文章鏈接,另外queue聲明成全局變量($),以便在函數(shù)中也可以訪(fǎng)問(wèn)到。
我的曾是土木人博客文章列表總共有8頁(yè),所以需要實(shí)現(xiàn)給page_nums賦值為8
復(fù)制代碼 代碼如下:
#開(kāi)辟的線(xiàn)程數(shù)
threadNums = 10
threadNums.times do
threadsThread.new do
until $queue.empty?
url = $queue.pop(true) rescue nil
html = get_html(url)
fetch_links(html)
if !url.include?"?page"
title = Nokogiri::HTML(html).css('title').text
puts "["+ Time.now.strftime("%H:%M:%S") + "]「" + title + "」" + url
save_to("pages/" + title.gsub(/\//,""),html) if url.include?".html"
end
end
end
end
threads.each{|t| t.join}
通過(guò)Thread.new來(lái)創(chuàng)建線(xiàn)程
創(chuàng)建線(xiàn)程后,會(huì)進(jìn)入until $queue.empty?循環(huán),直到任務(wù)隊(duì)列為空(即:沒(méi)有要采集的網(wǎng)址了)
開(kāi)辟的線(xiàn)程,每次都會(huì)從任務(wù)隊(duì)列(queue)取到一個(gè)url,并通過(guò)get_html函數(shù)獲取網(wǎng)頁(yè)源碼
由于任務(wù)隊(duì)列中的url有分頁(yè)url和文章url兩種,所以要進(jìn)行區(qū)分。
如果是分頁(yè)url(url中含有“?page”),就直接提取文章鏈接。
如果是文章url,就保存到本地(save_to(),文件名為文章title)
在循環(huán)體外,創(chuàng)建線(xiàn)程完畢后,需要將創(chuàng)建的線(xiàn)程執(zhí)行Thread#join方法,以便讓主線(xiàn)程等待,
直到所有的線(xiàn)程執(zhí)行完畢才結(jié)束主線(xiàn)程
代碼執(zhí)行時(shí)間統(tǒng)計(jì)
復(fù)制代碼 代碼如下:
#程序開(kāi)始的時(shí)間
$total_time_begin = Time.now.to_i
#執(zhí)行過(guò)程
#程序結(jié)束的時(shí)間
$total_time_end = Time.now.to_i
puts "執(zhí)行時(shí)間:" + ($total_time_end - $total_time_begin).to_s + "秒"
TIme模塊的#now方法可以獲取當(dāng)前時(shí)間,然后使用to_i,可以將當(dāng)前時(shí)間轉(zhuǎn)換成從1970年1月1日00:00:00 UTC開(kāi)始所經(jīng)過(guò)的秒數(shù)。
獲取網(wǎng)頁(yè)源碼
復(fù)制代碼 代碼如下:
#獲取網(wǎng)頁(yè)源碼
def get_html(url)
html = ""
open(url) do |f|
html = f.read
end
return html
end
ruby中,獲取網(wǎng)頁(yè)的方法用Net::HTTP模塊和OpenURI模塊。OpenURI模塊最簡(jiǎn)單,可以直徑將指定網(wǎng)頁(yè)當(dāng)成普通文件一樣進(jìn)行操作。
執(zhí)行結(jié)果:使用多線(xiàn)程采集130多篇文章,耗時(shí)15秒(單線(xiàn)程:47s左右)
您可能感興趣的文章:- Ruby中使用mechanize批量下載校內(nèi)網(wǎng)相冊(cè)照片
- ruby實(shí)現(xiàn)的一個(gè)異步文件下載HttpServer實(shí)例
- 比較不錯(cuò)的關(guān)于ruby的電子書(shū)下載地址集合
- windows和linux下Ruby的下載與安裝
- Ruby使用eventmachine為HTTP服務(wù)器添加文件下載功能