Skip to content

Instantly share code, notes, and snippets.

@iseebi
Last active September 22, 2019 15:45
Show Gist options
  • Save iseebi/9ca07a2ca53cbb172465344c02a787cd to your computer and use it in GitHub Desktop.
Save iseebi/9ca07a2ca53cbb172465344c02a787cd to your computer and use it in GitHub Desktop.
技術書典かんたん後払いのデータをスクレイピング

使い方

  1. 頒布物支払状況 のページを開く
  2. Webインスペクター(DevTools)等を開き、<div class="app-conatiner"> の中身をコピー(<h3>頒布物支払状況</h3>の直上の要素)
  3. <html><body>(2.でコピーした内容)</body></html> の内容で atobarai.html に保存。parse_atobarai.rb と同じ位置に配置する
  4. parse_atobarai.rb のあるところをカレントディレクトリにして、ruby parse_atobarai.rb を実行。JSONで出力される。

あとはjqとかシェル芸とかでなんとでも。例えばこんな感じ。

$ ruby atobarai_parse.rb | jq -r '.rows[] | [.status, .created_at, .fee_rate, .fee, .items[0].name, .items[0].count, .items[0].price, .items[1].name, .items[1].count, .items[1].price] | @csv' > atobarai.csv
require 'hpricot'
require 'json'
text = File.open('atobarai.html') {|f| f.read }
doc = Hpricot(text)
rows = []
(doc/"mat-expansion-panel").each do |row|
row_data = {}
details = (row/".mat-list-item-content")[0]
(details/".mat-line").each do |line|
line.inner_text.scan(/\s*([^:]*):\s*([^,\n]*),?/).each do |m|
if m[0] == "シェアコード"
row_data[:share_code] = m[1]
elsif m[0] == "ステータス"
row_data[:status] = m[1]
elsif m[0] == "作成日時"
row_data[:created_at] = m[1]
elsif m[0] == "キャンセル期限"
row_data[:cancel_limit] = m[1]
elsif m[0] == "サークル側手数料率"
row_data[:fee_rate] = m[1]
elsif m[0] == "サークル側手数料合計"
row_data[:fee] = m[1].gsub(/¥/, '').chomp.to_i
else
end
end
end
row_data[:items] = []
(row/".mat-list-item-content").drop(1).each do |item|
name = (item/"h4").inner_text
price_line = (item/"p").inner_text
m = price_line.match(/(\d)つ\s*¥(\d+)/)
count = m[1].to_i
price = m[2].to_i
row_data[:items] << {:name => name, :count => count, :price => price}
end
rows << row_data
end
puts ({:rows => rows}).to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment