読者です 読者をやめる 読者になる 読者になる

ニコニコ動画で検索してURL表示するスクレイピング

~/bin/nico

#!/usr/bin/ruby -wKU

require 'net/https'
require 'kconv'
require 'rubygems'
require 'mechanize'


OPT = {:hot => false, :new => false}
action = ARGV.shift
if action && !OPT.has_key?(action.to_sym)
  $stderr.puts "no such subcommand[#{OPT.keys.join(',')}]: #{action}"
  exit 1
else
  OPT[action.to_sym] = true
end

base_url = "http://www.nicovideo.jp/"
search_url = base_url + "search/"
search_url += URI.encode(ARGV.join('+'))
search_url += "?sort=v" if OPT[:hot]

puts "search url is #{search_url}"

agent = WWW::Mechanize.new
login_page = agent.get("http://www.nicovideo.jp/")
login_form = login_page.forms.first

login_form['mail'] = "メールアドレス"
login_form['password'] = "パスワード"

redirect_page = agent.submit(login_form)
doc = agent.get(search_url)

list = doc.links.map do |l|
  if(l.href &&
      l.href.include?("watch/") &&
      l.text && !(l.text.empty?))
    [l.text, (base_url+l.href)]
  end
end.compact

music_list = list if OPT[:new] || OPT[:hot]
music_list.each{|l| puts l.first; puts l.last; puts}

agent.get("http://www.nicovideo.jp/logout/")
nico hot 作業用BGM 邦楽

などするとよいですね.