Last active
September 5, 2016 12:38
-
-
Save ryosuke-endo/5cef89a6536237a3e3f16b235472b1e2 to your computer and use it in GitHub Desktop.
parallerのサンプル 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'nokogiri' | |
require 'benchmark' | |
Benchmark.bm do |r| | |
r.report do | |
pages = {} | |
[*1..100].each do |no| | |
url = "http://matome.naver.jp/search?q=%e5%90%8d%e8%a8%80&page=#{no}" | |
doc = Nokogiri::HTML.parse(open(url)) | |
witticisms = {} | |
doc.css('.mdMTMTtlList03Txt').each_with_index do |node, index| | |
text = node.css('.mdMTMTtlList03Ttl').text.gsub(/\s/, '') | |
favorite = node.css('.mdSocialCountList01Li.mdSocialCountList01FV').text.gsub(/\s/, '') | |
count = node.css('.mdSocialCountList01Li.mdSocialCountList01View').text.gsub(/\s|\|/, '') | |
witticisms["#{index}"] = { text: text, favorite: favorite, count: count } | |
end | |
pages["#{no}"] = witticisms | |
end | |
File.open('result.txt', 'a') do |f| | |
pages.each do |no, witticism| | |
f.puts "#{no}, #{witticism}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment