欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ruby中使用多線程隊(duì)列(Queue)實(shí)現(xiàn)下載博客文章保存到本地文件

 更新時(shí)間:2015年01月22日 11:24:06   投稿:junjie  
這篇文章主要介紹了Ruby中使用多線程隊(duì)列(Queue)實(shí)現(xiàn)下載博客文章保存到本地文件,本文給出了實(shí)現(xiàn)代碼、并對(duì)代碼的核心部分做了講解,同時(shí)給出了運(yùn)行效果圖,需要的朋友可以參考下

Ruby:多線程下載博客文章到本地的完整代碼

復(fù)制代碼 代碼如下:

#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 + "秒"

多線程部分講解

復(fù)制代碼 代碼如下:

$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

復(fù)制代碼 代碼如下:

#開辟的線程數(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ì)

復(fù)制代碼 代碼如下:

#程序開始的時(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)頁源碼

復(fù)制代碼 代碼如下:

#獲取網(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)文章

最新評(píng)論