Created
November 25, 2011 12:27
-
-
Save ainame/1393413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| ####################################### | |
| # 将棋の棋譜でーたべーすのidのリスト | |
| # を使って棋譜をぶっこぬくスクリプト | |
| ####################################### | |
| require 'open-uri' | |
| require 'nkf' | |
| require 'nokogiri' | |
| File.open("./kid.list","r") do |f| | |
| `mkdir -p tmp-kifu` | |
| while (line = f.gets) | |
| kid = line.chomp | |
| puts kid | |
| url = "http://wiki.optus.nu/shogi/index.php?cmd=kif&cmds=displaytxt&kid=#{kid}" | |
| #page = NKF.nkf('-w', open(url).read) | |
| try_count = 0 | |
| begin | |
| page = open(url, | |
| "User-Agent" => | |
| "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; ja-JP-mac; rv:1.9.0.6) Gecko/2009011912 Firefox/3.0.6" | |
| ).read | |
| rescue | |
| if try_count < 10 | |
| try_count += 1 | |
| retry | |
| end | |
| raise "10回失敗" | |
| end | |
| if page.to_s == "" | |
| raise "取得できなかった" | |
| end | |
| doc = Nokogiri::HTML(page) | |
| doc.search('textarea').each do |t| | |
| File.open("./tmp-kifu/#{kid}.kif","w") do |out| | |
| out.puts t.text | |
| end | |
| end | |
| sleep 2 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment