-
Notifications
You must be signed in to change notification settings - Fork 3
/
多线程.rb
67 lines (60 loc) · 1.45 KB
/
多线程.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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 + "秒"