Skip to content

Instantly share code, notes, and snippets.

@IshidaMotohiro
Created January 22, 2016 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IshidaMotohiro/3f24e4fa4e5203726851 to your computer and use it in GitHub Desktop.
Save IshidaMotohiro/3f24e4fa4e5203726851 to your computer and use it in GitHub Desktop.
ウィキペディアからのデータ取得
---
title: "ウィキペディアからのデータ取得"
author: "Ishida Motohiro"
date: "2016年1月22日"
output: html_document
---
ウィキペディアの[花札](https://ja.wikipedia.org/wiki/%E8%8A%B1%E6%9C%AD) から札の一覧を**表**形式のデータとして取得する。
# 追加パッケージの導入
WEBサイトの情報を効率的に取得できるパッケージをインストールする。
```{r echo = FALSE, message = FALSE}
if (!require(rvest)) {install.packages("rvest"); require(rvest)}
if (!require(dplyr)) {install.packages("dplyr"); require(dplyr)}
```
# 花札のぺーじを取得
## Mac あるいは Linux の場合
```{r message = FALSE}
wiki <- read_html("http://ja.wikipedia.org/wiki/%E8%8A%B1%E6%9C%AD")
hana <- html_table(html_nodes(wiki, "table")[[3]])
hana
```
## Windowsの場合
Windowsの場合、文字コードの関係で、上記の命令ではエラーとなります。
以下のコードに置き換えて実行してください。
```
wiki <- read_html("http://ja.wikipedia.org/wiki/%E8%8A%B1%E6%9C%AD")
hanahuda <- wiki %>% html_nodes("table") %>% .[[3]] %>% html_nodes("td") %>% html_text()
dim(hanahuda) <- c(6,12)
hanahuda %>% t %>% as.table #as.table (t(hanahuda))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment