Last active
January 4, 2016 19:29
-
-
Save kimihito/8667565 to your computer and use it in GitHub Desktop.
すいませんすいませんすいませんすいません
This file contains 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
#!/usr/bin/env ruby | |
#-*- coding: utf-8 -*- | |
require 'mechanize' | |
#お笑いナタリーの芸人プロフィールのページから、芸人の情報を抜き出すスクリプト | |
#期待するdataの中身 | |
=begin | |
data = | |
{ | |
0: { | |
group_name: "コンビ名", | |
member: { | |
0: { | |
#全部揃っているとは限らない | |
name: "名前", | |
birth: "生年月日", | |
origin: "出身地", | |
belong: "所属事務所" | |
}, | |
1: { | |
name: "名前", | |
birth: "生年月日", | |
belong: "所属事務所" | |
}, ... | |
} | |
}, | |
1:{ | |
group_name: "コンビ名", | |
member: { | |
.... | |
} | |
}, | |
.... | |
} | |
=end | |
agent = Mechanize.new | |
page = agent.get('http://natalie.mu/owarai/artist/list/order_by/sort_name') | |
profile_links = page.links_with(text: 'プロフィール') | |
data = {} | |
profile_links.each_with_index do |link,index| | |
#各芸人のプロフィールを抜き出す | |
profile_page = link.click | |
profile = {} | |
group_name = profile_page.search("div[@id='item-name'] > h2").inner_text | |
profile[:group_name] = group_name | |
#プロフィール | |
members = profile_page.search("div[@id='item-profile'] > p").children().text().gsub("\n", "").split("\r") | |
member_profile = {} | |
name_list = members.group_by{ |member|member.include?("●") }[true] #=> ["名前1","名前2"...] | |
origin_list = members.group_by{ |member|member.include?("出身地") }[true] #=> ["出身地1","出身地2"...] | |
birth_list = members.group_by{ |member|member.include?("生年月日") }[true] #=> ["生年月日1","生年月日2"...] | |
belong_list = members.group_by{ |member|member.include?("所属") }[true] #=> ["所属事務所","所属事務所"...] | |
#member_profileの中身を | |
=begin | |
member_profile = { | |
0: { | |
#全部揃っているとは限らない | |
name: "名前", | |
birth: "生年月日", | |
origin: "出身地", | |
belong: "所属事務所" | |
}, | |
1: { | |
name: "名前", | |
birth: "生年月日", | |
belong: "所属事務所" | |
}, ... | |
} | |
にしたいけど、ここがわからない。 | |
=end | |
#関連リンク集 | |
links = {} | |
links_info = profile_page.search("div[@id='item-link'] > ul > li") | |
links_info.each_with_index do |link,index| | |
link_data = {} | |
title = link.text | |
url = link.search('a').attribute('href').value() | |
link_data[:title] = title | |
link_data[:url] = url | |
links[index.to_s.to_sym] = link_data | |
end | |
profile[:links] = links | |
data[index.to_s.to_sym] = profile | |
end |
メンバー1人しか居ない場合、●なまえ
がなくて本名:なまえ
になってますね。これだとscan(/^●[^●]+/)
の正規表現使えないですねすみません
すごく関係ないけどファイル名たぶんscraping.rb
ですね
フォーマットが統一されてなくてだいぶスクレイピングしづらい感ありますね。
http://natalie.mu/owarai/artist/7398
http://natalie.mu/owarai/artist/6268
http://natalie.mu/owarai/artist/6297
- プロフィールページへのリンクが含まれたページを取得
- プロフィールページを取得
- プロフィールページのフォーマットを見極めていくつかの方法を切り替えながらスクレイピング、謎のフォーマットがあったらログを吐く
みたいな手順を踏むといいのかなと思いました
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
group_by{|a|}[true]
はselect{|a|}
って書けますが、どちらにせよデータが一部抜けていた場合name_list = [田中, 太田]
origin_list = [田中の出身地]
birth_list = [太田の出身地]
となったりしてどうしようもないので、こうならないように処理を変える必要があります。
例1
split(/\r/)
で行毎に分ける前に、人毎に分けるsplit(/●/)
scan(/^●[^●]+/)
例2
eachで回して、●がきたら新しいデータを作るようにする方法もあります
member_profile
は配列のほうがmember_profile << でーた
とかできて便利だと思います