Created
October 25, 2017 15:10
-
-
Save taaaaho/02773c6fb44132b3a980faa263af8d50 to your computer and use it in GitHub Desktop.
はてなブログの記事一覧を取得する
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 'nokogiri' | |
require 'open-uri' | |
url = '[ここにはてなブログのURLを指定]' | |
charset = nil | |
continue = true; | |
while continue do | |
continue = false; | |
html = open(url) do |f| | |
charset = f.charset | |
f.read | |
end | |
doc = Nokogiri::HTML.parse(html, nil, charset) | |
doc.xpath('//a[@class="entry-title-link"]').each do | node | | |
p node.get_attribute(:href) | |
p node.inner_text | |
# p node.inner_text + "," + node.get_attribute(:href) | |
end | |
doc.xpath('//span[@class="pager-next"]/a').each do | node | | |
url = node.get_attribute(:href) | |
continue = true; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment