Ruby中使用多線程隊列(Queue)實現(xiàn)下載博客文章保存到本地文件
Ruby:多線程下載博客文章到本地的完整代碼
#encoding:utf-8
require 'net/http'
require 'thread'
require 'open-uri'
require 'nokogiri'
require 'date'
$queue = Queue.new
#文章列表頁數(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)頁源碼
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
#程序開始的時間
$total_time_begin = Time.now.to_i
#開辟的線程數(shù)
threadNums = 10
threadNums.times do
threads<<Thread.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é)束的時間
$total_time_end = Time.now.to_i
puts "線程數(shù):" + threadNums.to_s
puts "執(zhí)行時間:" + ($total_time_end - $total_time_begin).to_s + "秒"
多線程部分講解
$queue = Queue.new
#文章列表頁數(shù)
page_nums = 8
page_nums.times do |num|
$queue.push("http://www.cnblogs.com/hongfei/default.html?page="+num.to_s)
end
首先聲明一個Queue隊列,然后往隊列中添加文章列表頁,以便后面可以從這些列表頁中提取文章鏈接,另外queue聲明成全局變量($),以便在函數(shù)中也可以訪問到。
我的曾是土木人博客文章列表總共有8頁,所以需要實現(xiàn)給page_nums賦值為8
#開辟的線程數(shù)
threadNums = 10
threadNums.times do
threads<<Thread.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}
通過Thread.new來創(chuàng)建線程
創(chuàng)建線程后,會進入until $queue.empty?循環(huán),直到任務隊列為空(即:沒有要采集的網(wǎng)址了)
開辟的線程,每次都會從任務隊列(queue)取到一個url,并通過get_html函數(shù)獲取網(wǎng)頁源碼
由于任務隊列中的url有分頁url和文章url兩種,所以要進行區(qū)分。
如果是分頁url(url中含有“?page”),就直接提取文章鏈接。
如果是文章url,就保存到本地(save_to(),文件名為文章title)
在循環(huán)體外,創(chuàng)建線程完畢后,需要將創(chuàng)建的線程執(zhí)行Thread#join方法,以便讓主線程等待,
直到所有的線程執(zhí)行完畢才結(jié)束主線程
代碼執(zhí)行時間統(tǒng)計
#程序開始的時間
$total_time_begin = Time.now.to_i
#執(zhí)行過程
#程序結(jié)束的時間
$total_time_end = Time.now.to_i
puts "執(zhí)行時間:" + ($total_time_end - $total_time_begin).to_s + "秒"
TIme模塊的#now方法可以獲取當前時間,然后使用to_i,可以將當前時間轉(zhuǎn)換成從1970年1月1日00:00:00 UTC開始所經(jīng)過的秒數(shù)。
獲取網(wǎng)頁源碼
#獲取網(wǎng)頁源碼
def get_html(url)
html = ""
open(url) do |f|
html = f.read
end
return html
end
ruby中,獲取網(wǎng)頁的方法用Net::HTTP模塊和OpenURI模塊。OpenURI模塊最簡單,可以直徑將指定網(wǎng)頁當成普通文件一樣進行操作。
執(zhí)行結(jié)果:使用多線程采集130多篇文章,耗時15秒(單線程:47s左右)
相關(guān)文章
ruby實現(xiàn)的一個異步文件下載HttpServer實例
這篇文章主要介紹了ruby實現(xiàn)的一個異步文件下載HttpServer,使用eventmachine和em-http-server實現(xiàn),需要的朋友可以參考下2014-07-07Python的Flask框架中SERVER_NAME域名項的配置教程
SERVER_NAME項在Flask的路由配置中至關(guān)重要,特別是在配置絕對url和子域名的操作中,這里我們就來看一下Python的Flask框架中SERVER_NAME項的配置教程:2016-06-06Ruby日期時間的比較,日期轉(zhuǎn)換等時間日期處理方法大全
這篇文章主要介紹了Ruby的日期時間處理方法與函數(shù),日期時間和字符串、數(shù)值之間的轉(zhuǎn)換,日期時間的比較需要的朋友可以參考下2022-04-04詳解組合模式的結(jié)構(gòu)及其在Ruby設計模式編程中的運用
這篇文章主要介紹了組合模式的結(jié)構(gòu)及其在Ruby設計模式編程中的運用,組合模式在Ruby on Rails框架開發(fā)項目中也經(jīng)常被用到,需要的朋友可以參考下2016-03-03Ruby on Rails實現(xiàn)最基本的用戶注冊和登錄功能的教程
這里我們主要以has_secure_password的用戶密碼驗證功能為中心,來講解Ruby on Rails實現(xiàn)最基本的用戶注冊和登錄功能的教程,需要的朋友可以參考下2016-06-06