Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created January 31, 2011 23:42
Show Gist options
  • Save hitode909/805079 to your computer and use it in GitHub Desktop.
Save hitode909/805079 to your computer and use it in GitHub Desktop.
ブサ彦物語ダウンロードする
# -*- coding: utf-8 -*-
require 'open-uri'
require 'nokogiri'
require 'uri'
$index_url = URI.parse('http://mbbs.tv/u/?id=18over')
$known_pages = []
def page(url)
Nokogiri open(url).read.force_encoding('sjis')
end
def abs(url)
($index_url + url).to_s
end
def write_file(title, body)
open("#{title}.txt", "w") {|f|
f.write body
}
end
def topic_page(url)
container = page(url).at('div[align="LEFT"]')
body = container.text.gsub("[←前][次→] back", "").gsub(/\r/, '').gsub(/^\n+/, '')
title = body.lines.first.chomp
p title
write_file(title, body)
end
def index_page(url)
return if $known_pages.include? url
$known_pages << url
page(url).search('a').each{|a|
case a['href']
when /p=\d+/
index_page(abs(a['href']))
when /tid=\d+/
topic_page(abs(a['href']))
end
}
end
index_page($index_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment