027-Crawler出来そうな気がしてきた
昨日書いたcrawler.rbから得られる情報を、MongoDBに格納するという一連の流れを確認するために「entry.rb」「main.rb」を下記のように書きました
# entry.rb require 'rubygems' require 'mongoid' Mongoid.configure do |config| name = "test" host = "localhost" config.master = Mongo::Connection.new.db(name) config.slaves = [ Mongo::Connection.new(host, 27017, :slave_ok => true).db(name) ] config.persist_in_safe_mode = false end class Entry include Mongoid::Document field :permalink field :title field :html_body end
# main.rb require 'rubygems' require 'crawler.rb' require 'entry.rb' url = "http://blog.pasonatech.co.jp/counselor/career_blog/399/15369.html" crawler = Crawler.new(url) entry = Entry.new() entry.permalink = url #とりあえずの処理なのであとで修正必要 entry.title = crawler.set_title entry.html_body = crawler.set_html_body entry.save
main.rbを実行して,mongoのコマンドラインで確認したらしっかりとMongoDBに値が入っていたー
$mongo $MongoDB shell version: 1.6.6-pre- connecting to: test > db.entries.find() { "_id" : ObjectId("4d3f584de93ac168d2000001"), "permalink" : "http://blog.pasonatech.co.jp/counselor/career_blog/399/15369.html", "html_body" : "<p> 〜中略〜 </p>", "title" : "003:育児 ベビーサイン" }
次回書きたいこと
サイト巡回する時にどのページまで取得しているかを管理する方法