パスワードを忘れた? アカウント作成
725943 journal

Silphireの日記: comments.plからタイトル部分を抜き出す

日記 by Silphire
#!/usr/bin/ruby -Ke

require 'net/http'
require 'kconv'

# gethtml
#  server: サーバ
#  path:   `/'からの絶対パス
def gethtml(server, path)

  # 初期化
  s = ""
  Net::HTTP.version_1_2

  Net::HTTP.start(server, 80) do |http|
    # HTMLを取得
    res , = http.get(path)

    # `\n'や複数のスペースを一つのスペースに
    s = res.body.gsub(/\s+/m, ' ')
  end
  s
end

# getindexで使う正規表現群
START_COMMENT = /.*<!-- start template: ID 84, discuss_list;comments;default -->/
END_COMMENT   = /<!-- end template: ID 84, discuss_list;comments;default -->.*/
TITLE_PATTERN = /<LI><A HREF=".*?">(.*?)<\/A> \(<A HREF="(.*?)">/

# getindex
#  comments.plからタイトルとURLを取得
#  hash{ "title"=>, "url"=> }
def getindex
  info = []

  # HTML取得
  s = gethtml('slashdot.jp', '/comments.pl')

  # 前と後の余分な物を削除
  s.gsub!(START_COMMENT, '')
  s.gsub!(END_COMMENT, '')

  # タイトルとURLを抜き出す
  s.scan(TITLE_PATTERN) do |t|
    h = {}
    h['title'] = t[0]
    h['url']   = t[1]
    info << h
  end

  info
end

getindex.each do |h|
  print "#{h['title'].tosjis} <#{h['url']}>\n"
end
この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

Stay hungry, Stay foolish. -- Steven Paul Jobs

読み込み中...