Ruby:多线程队列(Queue)下载博客文章到本地
来源:互联网 发布:薛之谦地铁事件知乎 编辑:程序博客网 时间:2024/05/16 03:38
Ruby:多线程下载博客文章到本地的完整代码
#encoding:utf-8require 'net/http'require 'thread'require 'open-uri'require 'nokogiri'require 'date'$queue = Queue.new#文章列表页数page_nums = 8page_nums.times do |num| $queue.push("http://www.cnblogs.com/hongfei/default.html?page="+num.to_s)endthreads = []#获取网页源码def get_html(url) html = "" open(url) do |f| html = f.read end return htmlenddef 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 endenddef 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 = 10threadNums.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 endendthreads.each{|t| t.join}#程序结束的时间$total_time_end = Time.now.to_iputs "线程数:" + threadNums.to_sputs "执行时间:" + ($total_time_end - $total_time_begin).to_s + "秒"
多线程部分讲解
$queue = Queue.new#文章列表页数page_nums = 8page_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 = 10threadNums.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 endendthreads.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_iputs "执行时间:" + ($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 htmlend
ruby中,获取网页的方法用Net::HTTP模块和OpenURI模块。OpenURI模块最简单,可以直径将指定网页当成普通文件一样进行操作。
执行结果:使用多线程采集130多篇文章,耗时15秒(单线程:47s左右)
推荐阅读:
Thread and Queue
Ruby线程学习:并行发起http请求
Ruby线程:关于线程同步的问题
Thread#join的作用
线程同步:MonitorMixin
3种方式实现线程同步
[Ruby]线程和进程
Ruby的OpenURI模块
Ruby: Net::Http and open-uri
- Ruby:多线程队列(Queue)下载博客文章到本地
- python_爬取博客文章下载到本地
- 下载博客文章并自动转换成pdf保存到本地
- 【Python】多线程下载韩寒博客文章
- Java多线程之队列Queue
- 微信文章图片防盗链,下载到本地
- 使用Python urllib2下载CSDN博客列表到本地
- Windows Live Writer博客发布工具测试在本地发布博客文章到csdn
- Unity3d Thread多线程,Queue队列,lock关键字
- Python 多线程|Queue队列|生产者消费者模式|
- python实现下载韩寒博客中的所有文章,在本地存储
- ASIHTTPRequest 下载到本地
- 下载文件到本地
- 视频下载到本地
- python多线程-queue队列类型优先级队列,FIFO,LIFO
- php正则匹配文章中的远程图片地址并下载图片到本地
- php正则匹配文章中的远程图片地址并下载图片到本地
- python多线程+队列下载资源
- Dreamware CC开发网页小问题汇总
- Django学习(基础入门上篇)
- 怎样才能成为一名合格的数据分析师?
- int类型的数值表示数的范围
- 88. Merge Sorted Array
- Ruby:多线程队列(Queue)下载博客文章到本地
- 玲珑学院OJ 1129 喵哈哈村的战斗魔法师丶坏坏い月【线段树查询最左端大于某个数的操作】
- 解决JS中乘法的浮点错误的方法
- PS常用快捷键
- Ubuntu16.04+cuda8.0+caffe安装教程
- windows中常用的运行命令(未完成)
- 美团2017秋招编程题
- C++虚函数
- MyBatis和Hibernate相比,优势在哪里