class: center, middle, inverse, title-slide # R Club - Lesson 6 ##
http://s.opur.club/18d6
### Kenji Sato ### 2018/12/19 & 26 --- $$\let\oldhat\hat \renewcommand{\hat}[1]{\oldhat{\hspace{0pt} #1}}$$ <div style="margin-top: -2.5em"></div> ## トピック - HTML の基本 - rvest による Web スクレイピング --- ## 準備 ```r install.packages("servr") install.packages("rvest") ``` --- ## HTML の基本構造 ウェブサイトは HTML という書式ルールを使って書く。 次のような文書をしている。 ```html <!DOCTYPE html> <html lang="ja"> <head> <!-- ここに設定情報など --> </head> <body> <!-- ここにコンテンツ --> </body> </html> ``` --- ## タグ `<tag> ... </tag>` は文書内の役割を表現する。 - `<html></html>` HTML文書 - `<head></head>` ヘッダ - `<body></body>` 本文 - `<h1></h1>`, `<h2></h2>` ... 見出し - `<p></p>` 段落 - `<a></a>` ハイパーリンク, etc. etc.... --- ## 作ってみよう! R プロジェクトを開いて次のコマンドを実行 ```r dir.create("site") # site フォルダ作成 file.edit("site/index.html") # index.html ファイル作成 ``` 「Terminal」で R を起動, web サーバーを立ち上げる。 ```r $ R ... > servr::httd("site") To stop the server, run servr::daemon_stop("140.....") or restart your R session Serving the directory ..../site at http://127.0.0.1:4321 ``` --- ## Web サーバーを立ち上げる Web サーバーというのは,ブラウザなどのソフトウェアから要求を受けてHTMLファイルなどの情報を送り返すサービスのこと。 今,みなさんが作った `site` フォルダ上においたファイルをウェブブラウザで見ることができるようになりました: <http://127.0.0.1:4321> `127.0.0.1` というのは自分自身のコンピュータを指す特別なアドレス。`:4321` はポート番号(普通は 80 だけどテストのため特別な番号を割り当てている) --- サンプル ```html <!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <h1>R Club</h1> <p> R Club は R の勉強会です。 </p> <p> 本日はウェブサイトの作り方を勉強しています。 </p> <ul> <li>HTML</li> <li>CSS</li> <li>JavaScript</li> </ul> </body> </html> ``` --- ## リンク 次のように修正(a タグの追加)してください。 ```html <ul> <li><a href="about-html.html">HTML</a></li> <li>CSS</li> <li>JavaScript</li> </ul> ``` ファイルを作ります。 ```r file.edit("site/about-html.html") ``` `<body></body>` の中に次のように書いてください。 --- ```html <table> <thead> <tr> <th>Tag</th> <th>役割</th> </tr> </thead> <tbody> <tr> <td>table</td> <td>表</td> </tr> <tr> <td>tr</td> <td>表の行</td> </tr> <tr> <td>th</td> <td>表のヘッダ</td> </tr> <tr> <td>td</td> <td>表の項目</td> </tr> </tbody> </table> ``` --- ## 属性 `<a></a>` に対する `href` のようなもの。 `class` と `id` が特に重要。 - `class` はグループ分け。複数のタグが同じ `class` 属性を持てる - `id` は一意な識別。複数のタグが同じ `id` 属性を持てない。 ```r file.edit("site/about-css.html") file.edit("site/style.css") ``` --- `style.css` ``` .space { height: 1200px; } ``` --- `about-css.html` ``` <!DOCTYPE html> <html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>CSS</h1> <h2 id="basics">CSS の基本</h2> <div class="space"></div><!-- 無意味な空白 --> <h2 id="application">CSS の応用</h2> <div class="space"></div><!-- 無意味な空白 --> <h2 id="future">CSS の未来</h2> <div class="space"></div><!-- 無意味な空白 --> <a href="/">home</a> </body> </html> ``` <http://127.0.0.1:4321#application> --- ## CSS CSS はサイトのデザインをコントロールするもの。 HTMLファイルの `<head></head>` の中に, ``` <link rel="stylesheet" type="text/css" href="style.css"> ``` といった記述をすることで,CSSファイルに定義されたスタイルが適用される。 `style.css` に次の記述を追加してみよう。(1つ確認しては,消して次のものを追加する) ``` h1 { color: red; } h2 { color: red; } #application { color: red; } ``` --- ## CSS (続き) ドットは class, ハッシュタグは id 属性 タグの階層構造はスペースでつなぐ(最下層だけでもOK) 複数のルールに同じデザインを適用するときはコンマ ``` section p .red { color: red; /* などなど */ } table { border-collapse: collapse; } table th, table td { border: solid 1px black; } ``` `about-html.html` にこのCSSを適用してみよう。 --- ## 練習 CSS の適用ルールを練習できるウェブアプリ <http://flukeout.github.io/> HTML 文書からデータを取得するときにも似たような考え方を使う。 --- ## 要素の検証 <img src="images/inspect.png" width="474" height="400" style="display: block; margin: auto;" /> --- ## 要素の検証(つづき) <img src="images/xpath.png" width="840" height="400" style="display: block; margin: auto;" /> --- ## HTML 文書からデータ取得 ```r library(tidyverse) library(rvest) ``` マネジメント学類シラバス <http://www0.osakafu-u.ac.jp/syllabus/list01.aspx?CD1=2B11&CD2=03> --- ## 注意! DoS攻撃について **Denial of Service Attack** プログラム的にWebサーバーに大量にリクエストを送ってサーバーをダウンさせてしまう攻撃。 Web スクレイピングには意図せずサーバーを攻撃してしまう危険があるので注意すること。 ```r Sys.sleep(3) ``` ゆっくり実行すること。 robots.txt についても調べておくこと。 --- ## 準備 1. まずは要素を検証して 2. HTML の構造を見極める 3. データ取得の戦略を練る 4. コーディングする --- ## rvest ```r root <- "http://www0.osakafu-u.ac.jp/syllabus" mng_url <- file.path(root, "list01.aspx?CD1=2B11&CD2=03") mng <- read_html(mng_url) html_structure(mng) ``` ``` ## <html [xmlns]> ## <head#Head1> ## <title> ## {text} ## <link [rel, href, media, type]> ## <script [src, type]> ## <body> ## {text} ## <form#form1 [method, action]> ## {text} ## <input#__VIEWSTATE [type, name, value]> ## <input#__VIEWSTATEGENERATOR [type, name, value]> ## <div#container> ## {text} ## {comment} ## {text} ## <div#header> ## {text} ## <div.wapper> ## {text} ## <h1> ## {text} ## <a [href]> ## <img [src, alt, width, height]> ## <br> ## <span> ## <img [src, alt, width, height]> ## {text} ## {text} ## <a [href, target]> ## {text} ## {text} ## {text} ## {text} ## {comment} ## {text} ## <div#navigation> ## {text} ## <div.wapper> ## {text} ## <ul> ## <li> ## <a [href]> ## {text} ## {text} ## <li> ## <a [href]> ## {text} ## {text} ## <li> ## <a [href]> ## {text} ## {text} ## <li> ## <a [href]> ## {text} ## {text} ## {text} ## {text} ## {comment} ## {text} ## <div#contents> ## {text} ## <p#bc_navi> ## {text} ## <a [href]> ## {text} ## {text} ## <span#lbl_Pan> ## {text} ## {text} ## {text} ## <h2#icon_department_Csys> ## <strong> ## {text} ## {text} ## {text} ## <div.paragraph> ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <ol.ol1> ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## <li.li1> ## <a [onclick, href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## <div.Csys> ## {text} ## <h3> ## <span> ## <strong> ## {text} ## {text} ## <div> ## {text} ## <ol.ol1> ## <li.li1> ## <a [href]> ## {text} ## {text} ## {text} ## <p.pagetop> ## <a [href]> ## {text} ## {text} ## {text} ## {text} ## {text} ## {comment} ## {text} ## <div#footer> ## {text} ## <div.wapper> ## {text} ## <p> ## {text} ## <strong> ## {text} ## {text} ## {text} ## {text} ## {text} ## {text} ``` --- ## リンクのリスト ```r links <- html_nodes(mng, css = ".li1") head(links) ``` ``` ## {xml_nodeset (6)} ## [1] <li class="li1"><a href="list02.aspx?CD1=3&CD2=602"> ... ## [2] <li class="li1"><a href="list02.aspx?CD1=3&CD2=601"> ... ## [3] <li class="li1"><a onclick="popupWindow('Detail.aspx?CD= ... ## [4] <li class="li1"><a onclick="popupWindow('Detail.aspx?CD= ... ## [5] <li class="li1"><a onclick="popupWindow('Detail.aspx?CD= ... ## [6] <li class="li1"><a onclick="popupWindow('Detail.aspx?CD= ... ``` --- ## リンクの下の a タグ ```r link_atag <- html_nodes(mng, css = ".li1 a") head(link_atag) ``` ``` ## {xml_nodeset (6)} ## [1] <a href="list02.aspx?CD1=3&CD2=602">初年次ゼミナール一覧へ</a> ## [2] <a href="list02.aspx?CD1=3&CD2=601">教養科目一覧へ</a> ## [3] <a onclick="popupWindow('Detail.aspx?CD=262','',1100,620 ... ## [4] <a onclick="popupWindow('Detail.aspx?CD=274','',1100,620 ... ## [5] <a onclick="popupWindow('Detail.aspx?CD=527','',1100,620 ... ## [6] <a onclick="popupWindow('Detail.aspx?CD=543','',1100,620 ... ``` --- ## href 属性 ```r link_href <- html_attr(link_atag, "href") head(link_href, 20) ``` ``` ## [1] "list02.aspx?CD1=3&CD2=602" "list02.aspx?CD1=3&CD2=601" ## [3] "detail.aspx?CD=262" "detail.aspx?CD=274" ## [5] "detail.aspx?CD=527" "detail.aspx?CD=543" ## [7] "detail.aspx?CD=796" "detail.aspx?CD=812" ## [9] "detail.aspx?CD=824" "detail.aspx?CD=1084" ## [11] "detail.aspx?CD=1096" "detail.aspx?CD=1352" ## [13] "detail.aspx?CD=1396" "detail.aspx?CD=1408" ## [15] "detail.aspx?CD=1566" "detail.aspx?CD=1578" ## [17] "detail.aspx?CD=266" "detail.aspx?CD=278" ## [19] "detail.aspx?CD=535" "detail.aspx?CD=547" ``` `list02` で始まっているのはページ内リンクなので,除去。 ```r syllabus_url <- link_href[!startsWith(link_href, "list")] ``` --- ## 調べるアドレス一覧 ```r length(syllabus_url) ``` ``` ## [1] 372 ``` ```r head(syllabus_url, 20) ``` ``` ## [1] "detail.aspx?CD=262" "detail.aspx?CD=274" ## [3] "detail.aspx?CD=527" "detail.aspx?CD=543" ## [5] "detail.aspx?CD=796" "detail.aspx?CD=812" ## [7] "detail.aspx?CD=824" "detail.aspx?CD=1084" ## [9] "detail.aspx?CD=1096" "detail.aspx?CD=1352" ## [11] "detail.aspx?CD=1396" "detail.aspx?CD=1408" ## [13] "detail.aspx?CD=1566" "detail.aspx?CD=1578" ## [15] "detail.aspx?CD=266" "detail.aspx?CD=278" ## [17] "detail.aspx?CD=535" "detail.aspx?CD=547" ## [19] "detail.aspx?CD=800" "detail.aspx?CD=816" ``` --- ## 単一のページの構造 ミクロ経済学入門 <http://www0.osakafu-u.ac.jp/syllabus/detail.aspx?CD=5950> ページトップにある表を取得するには? 要素の検証をしてみよう! --- ## 例 ```r course <- "http://www0.osakafu-u.ac.jp/syllabus/detail.aspx?CD=5950" course_html <- read_html(course) course_tbl <- html_node(course_html, "table") course_tbl %>% html_node("#lbl_KAMOKU_NM") %>% html_text() ``` ``` ## [1] "ミクロ経済学入門" ``` ```r course_tbl %>% html_node("#lbl_GAKKI_NM") %>% html_text() ``` ``` ## [1] "前期授業" ``` --- ## 単一のページのデータ取得 ```r course_info <- function(url) { course_html <- read_html(url) course_tbl <- html_node(course_html, "table") ids <- c("#lbl_KAMOKU_NM", "#lbl_HAITO", "#lbl_WJ_NM", "#lbl_GAKKI_NM", "#lbl_KYOIN_NM") vapply(ids, function(id) { course_tbl %>% html_node(id) %>% html_text() }, character(1)) } course_info(course) ``` ``` ## #lbl_KAMOKU_NM #lbl_HAITO #lbl_WJ_NM ## "ミクロ経済学入門" "1" "火4" ## #lbl_GAKKI_NM #lbl_KYOIN_NM ## "前期授業" "宇野 浩司" ``` --- ## 全ページのデータ取得 `for` ループを使う。`Sys.sleep()` を忘れないように! ```r syllabus_sml <- file.path(root, syllabus_url[201:205]) len <- length(syllabus_sml) result <- tibble( title = vector("character", len), year = vector("character", len), period = vector("character", len), semester = vector("character", len), professor = vector("character", len) ) for (i in seq_along(syllabus_sml)) { result[i, ] = course_info(syllabus_sml[i]) Sys.sleep(3) } ``` --- ## 結果 ```r result ``` ``` ## # A tibble: 5 x 5 ## title year period semester professor ## <chr> <chr> <chr> <chr> <chr> ## 1 マネジメント学類演習I 3 水4 通年授業 酒井 貴子 ## 2 マネジメント学類演習I 3 金4 通年授業 平林 直樹 ## 3 マネジメント学類演習II 4 水4 通年授業 韓 池 ## 4 マネジメント学類演習II 4 水4 通年授業 日下 文男 ## 5 マネジメント学類演習II 4 火4 通年授業 鹿野 繁樹 ```