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