image

  • フォト Amazonギフト券
    ※この時計の時刻は、閲覧しているパソコンのものであり、必ずしも正確な時間とは限りません

検索

最近のトラックバック

無料ブログはココログ

« 2008-02-22 | トップページ | 2008-02-24 »

2008-02-23

2008-02-23

ruby mechanize の使い方の練習

社内の web ページを ruby mechanize で操作するための練習として、
mixi の "コミュニティ最新書き込み" を取得する ruby スクリプトを書いてみた。
(yield の使い方の練習もかねているが、こちらのほうは少し怪しい...)

実行結果例と、ソースコードを示す。(jruby でも動作する)

$ ruby getdialy.rb | nkf -w

2008年02月23日 17:28 三字熟語しりとり No.5(817) (言葉が好きだ。) view_bbs.pl?id=27488903&comment_count=817&comm_id=965
2008年02月23日 17:27 好きな映画を好きなだけ挙げて3本以上かぶったらマイミク!(885) (映画愛好会) view_bbs.pl?id=26387907&comment_count=885&comm_id=36
2008年02月23日 17:26 漢字しりとり No.28(771) (言葉が好きだ。) view_bbs.pl?id=27525724&comment_count=771&comm_id=965
... 省略 ...

$cat getdialy.rb
#
# See http://sora2hs.blog70.fc2.com/blog-entry-246.html

# mixi に login して、"コミュニティ最新書き込み" 一覧を得てから、logout する。
# username, password は config.yaml に書いておく。
#
# cat config.yaml
# user: name@gmail.com
# pass: pass
#

require 'rubygems'
require 'mechanize'
require 'yaml'
require 'rss'
require 'kconv'
require 'pp'

$KCODE = 'euc'

class Robot
  MIXI_URL = 'http://mixi.jp/'

  attr_reader :agent   # WWW::Mechanize

  # ログイン
  def login(config)
    # ユーザ名、パスワードを config.yaml に設定する
    config = YAML.load_file(config)
    username = config["user"]
    password = config["pass"]

    @agent = WWW::Mechanize.new
    page = @agent.get(MIXI_URL)
    form = page.forms[0]
    form.fields.find {|f| f.name == 'email'}.value = username
    form.fields.find {|f| f.name == 'password'}.value = password
    form.fields.find {|f| f.name == 'next_url'}.value = '/home.pl'
    page = @agent.submit(form, form.buttons.first)
    # p "-------- login --------"
  end

  # ログアウト
  def logout
    @agent.get('/logout.pl')
    # p "-------- logout --------"
  end

  # アクセス処理
  def Robot.open(config)
    robot = Robot.new
    robot.login(config)
    begin
       yield(robot)
    ensure
      robot.logout
    end
  end
end

def getData(robot)
  # diarylist = agent.get('/new_friend_diary.pl') # マイミクシィ最新日記
  diarylist = robot.agent.get('/new_bbs.pl') # コミュニティ最新書き込み
  doc = diarylist.root
  doc.search("/html/body/div[2]/div/div[2]/ul/li/dl").each {|entry|
    time = entry.at(:dt).inner_text.sub('?', ' ')
    dd = entry.at(:dd)
    title = dd.inner_text.strip
    url = dd.at(:a)["href"]
    print "#{time} #{title} #{url}¥n"
  }
end

robot = Robot.new
Robot.open('config.yaml') { |robot|
  getData(robot)
}

Imagyse37

ピックアップ: MS-IMEとかATOKとかSKKとか, 波って凍るんだ… , etc...

- http://www.moongift.jp/2008/02/enigmail/
> > MOONGIFT: » ThunderbirdでGPG暗号化「Enigmail」:オープンソースを毎日紹介

- http://gigazine.net/index.php?/news/comments/200800222_ms_open/
> > Windowsの技術情報などを非商用目的のオープンソース開発者に無料で公開 - GIGAZINE

- http://d.hatena.ne.jp/antipop/20080222/1203691403
> > MS-IMEとかATOKとかSKKとか - antipop

- http://d.hatena.ne.jp/mereco/20080222/p1
> > インコが百羽いる夢の銭湯に行ってきた - メレンゲが腐るほど恋したい

鳥インフルエンザの心配はないのか?

- http://coliss.com/articles/build-websites/operation/design/842.html
> > 画像をベクター化するVectorMagicが、対応フォーマットを増やしてバージョンアップ | コリス

- http://www.j-cast.com/2008/02/22017017.html
> > J-CASTニュース : さんま「あれが面白いんだ~!?」 「松本のすべらない話」を痛烈批評

- http://www.moongift.jp/2008/02/very_important/
> > MOONGIFT: » [重要なお知らせ] MOONGIFTにてウィルスが感知されました:オープンソースを毎日紹介

- http://nobi.cocolog-nifty.com/nobilog2/2008/02/post_9540.html
> > nobilog2: 教えることは学ぶこと。与えないことは教えること。

- http://coliss.com/articles/build-websites/operation/design/840.html
> > CSSとJavaScriptで実装するタブ型インターフェイス37選 -noupe | コリス

- http://labaq.com/archives/50926254.html
> > らばQ : え、波って凍るんだ…

mac ports で "unmatched open brace in list" のエラー

いまだに MacOSX 10.4 なのですが、
ruby の port が outdated になっていたので、
sudo port update ruby したら、途中で disk full になった。
その後、 port uninstall ruby とかしても、エラーが出て port 操作ができなくなってしまった。

$ sudo port -d uninstall ruby
DEBUG: unmatched open brace in list
    while executing
"array set receipt_$ref $receipt_contents"
    (procedure "receipt_flat::open_entry" line 84)
    invoked from within
"${macports::registry.format}::open_entry $name $version $revision $variants"
    (procedure "open_entry" line 4)
    invoked from within
"open_entry $iname $iversion $irevision $ivariants"
    (procedure "registry::installed" line 33)
    invoked from within
"registry::installed $portname $v"
    (procedure "portuninstall::uninstall" line 5)
    invoked from within
"portuninstall::uninstall $portname [composite_version $portversion [array get variations]] [array get options]"
Error: port uninstall failed: unmatched open brace in list

/opt/local/var/macports 以下にある ruby をすべて削除し、
sudo port -d selfupdate
sudo port -d sync
することで、直ったようだ。
現在、port install ruby +mactk を実行中...
# この作業で、ずいぶん 時間を費やしてしまった...

« 2008-02-22 | トップページ | 2008-02-24 »

mokuji

2013年12月
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        

google

  • twitter
  • __
  • _
    Googleボットチェッカー

合わせて読む

  • 合わせて読む
    フィードメーター - katoy: cocolog あわせて読みたい

リンク