- Rでスクレイピングできるようになる
- 今年度学んできたtidyverseな世界をモノにする
- 現場で実践できるようになる(試したくなる)
2018.12.15
"https://www.asahi.com/worldcup/schedule/" %>% read_html() %>% html_node(xpath = '//*[@id="GrpA"]/table') %>% html_table()
通称『宇宙本』。RStudioを使ってスクレイピングからレポーティングまでを網羅した一冊。今年度勉強会での基本的な内容をほぼ網羅 している。(技術評論社 )
スクレイピングは、Import に位置付けられる。
データソースから直接取り込むという点で、Import の更に前段も含んでいるという解釈も可能。
「素晴らしいじゃないか、ジョン。ところで以前渡したデータには間違いがあったんだ。」
引用:「Wonderful R 3 再現可能性のすゝめ」(共立出版)
手作業が発生した時点で、その後のプロセスの再現可能性は破綻している
スクレイピングする際には以下に気をつけましょう
→ API等を利用しよう
→ 対象データ、用途、公開先を適切に
→ 繰り返し処理で取得する場合は、Sleepする
基本的には自己責任です!
HTMLの要素を樹木のような階層構造に変換したもの
HTML内のスタイル(文字の色や大きさなど)を定義する。
今日の目的では深く知る必要はありませんが、免疫をつけてください。
<html> <head> <title>タイトル</title> </head> <body> <h1>大見出し</h1> <a href="http://hogehoge.jp/" target="_blank">ほげほげ</a> <p class="green">classを使った例</p> <p id="red">idを使った例</p> </body> </html>
Selectors Level 3 (W3C): https://www.w3.org/TR/css3-selectors/
h1 { font-style: italic; background: yellow; } p.green { color: green; } p#red { border: 1px solid; color: red; }
データを構造的に記録する
class, id属性による指定
XML Path Language 3.1 (W3C): https://www.w3.org/TR/xpath/
スクレイピングでは、CSSセレクタかXPathを使ってHTMLの要素にアクセスする
library(rvest)
"https://www.asahi.com/worldcup/schedule/" %>% read_html()
## {xml_document} ## <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja" dir="ltr" xmlns:og="http://ogp.me/ns#" xmlns:mixi="http://mixi-platform.com/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"> ## [1] <head>\n<!-- DTM上 --><script src="//assets.adobedtm.com/d7e679c95b1f ... ## [2] <body class="Wc2018-Schedule">\n<!-- googleoff:index -->\n\n<!-- Goo ...
"https://www.asahi.com/worldcup/schedule/" %>% read_html() %>% html_node(xpath = '//*[@id="GrpA"]/table')
## {xml_node} ## <table class="TableMod TableMatches" cellspacing="0" cellpadding="0" border="0" summary="サッカーW杯ロシア大会 グループA 試合日程"> ## [1] <thead class="ImgCenter"><tr>\n<th class="date">日時</th>\n<th colspan ... ## [2] <tbody class="ImgCenter">\n<tr class="RUS KSA">\n<td class="date">\n ...
"https://www.asahi.com/worldcup/schedule/" %>% read_html() %>% html_node(css = "title") %>% html_text()
## [1] "試合日程・結果|2018ワールドカップ(W杯)ロシア大会 - サッカー:朝日新聞デジタル"
"https://www.asahi.com/worldcup/schedule/" %>% read_html() %>% html_node(xpath = '//*[@id="GrpA"]/table') %>% html_table()
zip_url = page %>% html_nodes("a") %>% ## find all links html_attr("href") %>% ## pull out url str_subset("\\.zip") ## pull out zip links
関数 | 用途 |
---|---|
past0 | URLの切り貼りに |
strsplit | セパレータを指定して、文字列を分割する |
for | 複数のページから取得 |
Sys.sleep | 繰り返しアクセスする場合に間を空ける |
dplyr::mutate | データ型の指定など |
dplyr::bind_rows | 複数ページから取得したデータを連結する |
login_page <- html_session("http://hogehoge/redmine/login") login_form <- html_form(login_page)[[1]] %>% set_values(username="Kato", password="xxxxxxxxx") session <- submit_form(login_page, login_form)
session %>% jump_to("http://hogehoge/redmine/projects/prj-fugafuga/time_entries") %>% read_html() %>% html_node(xpath='//*[@id="content"]/form[2]/div[2]/table') %>% html_table()
プロジェクト | 日付 | ユーザー | 活動 | チケット | 時間 |
---|---|---|---|---|---|
プロジェクトA | 2018/09/13 | ほげほげ | テスト実施 | #6685: NERF0618 | 11.0 |
プロジェクトA | 2018/09/13 | ほげほげ | テスト実施 | #6693: NERF0628 | 7.5 |
プロジェクトA | 2018/09/12 | ぼけぼけ | テスト実施 | #6688: NERF0622 | 5.0 |
プロジェクトA | 2018/09/11 | 加藤 拓海 | レビュー | #6692: NERF0627 | 0.5 |
プロジェクトA | 2018/09/11 | ぼけぼけ | テスト実施 | #6688: NERF0622 | 4.0 |
プロジェクトA | 2018/09/10 | ほげほげ | テスト実施 | #6689: NERF0624 | 6.5 |
プロジェクトA | 2018/09/10 | ほげほげ | テスト実施 | #6692: NERF0627 | 3.0 |
プロジェクトA | 2018/09/07 | ぼけぼけ | レビュー | #6674: NERF0622 | 1.5 |
プロジェクトA | 2018/09/07 | ぼけぼけ | テスト実施 | #6692: NERF0627 | 2.0 |
プロジェクトA | 2018/09/07 | 加藤 拓海 | レビュー | #6679: NERF0628 | 0.5 |
チーム毎にテーマを選定してください。 以下、ご参考。
https://www.asahi.com/worldcup/schedule/
http://eco.mtk.nao.ac.jp/koyomi/dni/2018/s1301.html
チーム | リーダ | テーマ |
---|---|---|
A | Ka | |
B | Fu | |
C | Ma | |
D | No | |
E | Na |
なりふり構わず、果敢に乗り越えてください!
チーム | 発表者 | テーマ |
---|---|---|
A | ||
B | ||
C | ||
D | ||
E |
CC BY-NC-SA 4.0, Takumi Kato