-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathameblo.rb
62 lines (50 loc) · 1.38 KB
/
ameblo.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
# http://qiita.com/ya_s_u/items/c4f027df9be34fad8be5
# Made by @ya-s-u
require 'open-uri'
require 'nokogiri'
url = "http://ameblo.jp/:user_name/entrylist.html"
loop do
# html取得
charset = nil
html = open(url) do |f|
charset = f.charset
f.read
end
# 記事一覧のhtmlパース
doc = Nokogiri::HTML.parse(html, nil, charset)
# 記事一覧
doc.css('.contentTitleArea a').each do |node|
get_entry_url = node.attribute('href').value
d_html = open(get_entry_url) do |f|
charset = f.charset
f.read
end
# 記事のhtmlパース
entry_doc = Nokogiri::HTML.parse(d_html, nil, charset)
@entry_url = get_entry_url
@article_title = entry_doc.css('.skinArticleHeader2').text.gsub(/\s+/, '')
@article_time = entry_doc.css('.articleTime').text
@article_text = entry_doc.css('.articleText').text
# 記事のmarkdownを作成
def ameblo_post_md
<<~EOS
---
title: #{@article_title}
link: #{@entry_url}
time: #{@article_time}
---
#{@article_text}
EOS
end
File.open("posts/#{@article_title.gsub(/\//, '-')}.md", 'a' ) do |f|
f.write (ameblo_post_md)
end
end
# 次ページがなければ終了
unless doc.css('.pagingNext').empty?
url = doc.css('.pagingNext').attribute('href').value
p '---------------------------------------------'
else
break
end
end