麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Ruby > 正文

Ruby中使用多線程隊列(Queue)實現下載博客文章保存到本地文件

2020-10-29 19:42:21
字體:
來源:轉載
供稿:網友

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

復制代碼 代碼如下:

#encoding:utf-8
require 'net/http'
require 'thread'
require 'open-uri'
require 'nokogiri'
require 'date'

$queue = Queue.new
#文章列表頁數
page_nums = 8
page_nums.times do |num|
  $queue.push("http://www.cnblogs.com/hongfei/default.html?page="+num.to_s)
end

threads = []
#獲取網頁源碼
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

#開辟的線程數
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}

#程序結束的時間
$total_time_end = Time.now.to_i
puts "線程數:" + threadNums.to_s
puts "執行時間:" + ($total_time_end - $total_time_begin).to_s + "秒"

多線程部分講解

復制代碼 代碼如下:

$queue = Queue.new
#文章列表頁數
page_nums = 8
page_nums.times do |num|
  $queue.push("http://www.cnblogs.com/hongfei/default.html?page="+num.to_s)
end

首先聲明一個Queue隊列,然后往隊列中添加文章列表頁,以便后面可以從這些列表頁中提取文章鏈接,另外queue聲明成全局變量($),以便在函數中也可以訪問到。

我的曾是土木人博客文章列表總共有8頁,所以需要實現給page_nums賦值為8

復制代碼 代碼如下:

#開辟的線程數
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來創建線程

創建線程后,會進入until $queue.empty?循環,直到任務隊列為空(即:沒有要采集的網址了)
開辟的線程,每次都會從任務隊列(queue)取到一個url,并通過get_html函數獲取網頁源碼
由于任務隊列中的url有分頁url和文章url兩種,所以要進行區分。
如果是分頁url(url中含有“?page”),就直接提取文章鏈接。
如果是文章url,就保存到本地(save_to(),文件名為文章title)
在循環體外,創建線程完畢后,需要將創建的線程執行Thread#join方法,以便讓主線程等待,
直到所有的線程執行完畢才結束主線程

代碼執行時間統計

復制代碼 代碼如下:

#程序開始的時間
$total_time_begin = Time.now.to_i
#執行過程

#程序結束的時間
$total_time_end = Time.now.to_i
puts "執行時間:" + ($total_time_end - $total_time_begin).to_s + "秒"

TIme模塊的#now方法可以獲取當前時間,然后使用to_i,可以將當前時間轉換成從1970年1月1日00:00:00 UTC開始所經過的秒數。

獲取網頁源碼

復制代碼 代碼如下:

#獲取網頁源碼
def get_html(url)
  html = ""
  open(url) do |f|
    html = f.read
  end
  return html
end

ruby中,獲取網頁的方法用Net::HTTP模塊和OpenURI模塊。OpenURI模塊最簡單,可以直徑將指定網頁當成普通文件一樣進行操作。

執行結果:使用多線程采集130多篇文章,耗時15秒(單線程:47s左右)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 99视频在线观看视频 | 中文字幕极速在线观看 | 精品国产一区二区三区四 | 一级电影在线免费观看 | 91成人影库 | 亚洲小视频在线观看,com | 涩涩操| 亚洲一区二区免费 | 91久久夜色精品国产网站 | 精品中文字幕在线观看 | 日产精品一区二区三区在线观看 | 国产免费www| 国产手机av在线 | 中文字幕在线第二页 | 亚洲91精品 | 亚洲精品永久视频 | 欧美成人理论片乱 | 成人毛片视频免费看 | 久草成人在线 | 欧美精品一区二区久久 | 亚洲一区成人在线 | 国产在线精品一区二区夜色 | 露脸各种姿势啪啪的清纯美女 | 免费观看一级欧美大 | 国产精品视频六区 | 伊人久久电影网 | 久久国产精品网 | 亚洲精品午夜国产va久久成人 | av电影在线观看网址 | 精品国产精品久久 | 欧美成人免费 | 久久成人午夜视频 | 国产视频在线播放 | 久久影院一区二区三区 | 欧美老外a级毛片 | 黄色美女网站免费看 | 久久精品视频在线免费观看 | 久久精品99北条麻妃 | 69av导航 | 羞羞的网址| 一区二区三区视频播放 |