LoginSignup
36
41

More than 5 years have passed since last update.

rvest でログインしてスクレイピング #rstatsj

Last updated at Posted at 2015-08-07

ログインしないと見れないページをスクレイピングしたい。
rvest パッケージを使えばお手軽にできる。

試しに slideshare にログインしてみる。

R
library(rvest)

# ログイン状態のセッションを作る ------------------------------------------------------------
login_page <- html_session("https://www.slideshare.net/login")

login_form <- html_form(login_page)[[1]] %>% 
  set_values(user_login="hoxo_m@gmail.com", user_password="****")

session <- submit_form(login_page, login_form) 

# ログイン状態でスクレイピング ----------------------------------------------------
session %>% 
  jump_to("http://www.slideshare.net/") %>% 
  html_node(xpath='//*[@id="hp-hero"]/div[1]/div/a/h1/div[2]') %>%
  html_text
結果
[1] "Hoxo_m, how do you inspire creativity in yourself and others?"

hoxo_m でログインできていることがわかる。

それでは、ログインしないと見れない My uploads のページから、自分のスライドに関するデータをスクレイピングしてみる。

R
library(lambdaR)

data <- session %>%
  jump_to("http://www.slideshare.net/hoxo_m/edit_my_uploads?type=public") %>%
  html_nodes(xpath = '//div[starts-with(@id, "my-")]') %>%
  Map_(node: {
    title <- node %>%
      html_node(xpath='div[1]/div[2]/div[1]/div/a') %>%
      html_text
    data <- node %>% 
      html_nodes(xpath='div[1]/div[2]/div[3]/ul/li') %>% 
      html_text %>% 
      as.numeric
    result <- Reduce_(data, cbind, init = data.frame(title))
    colnames(result) <- c("title", "view", "like", "comment", "download")
    result
  }) %>%
  Reduce_(rbind)

knitr::kable(data)
title view like comment download
R で超簡単に並列処理を書けるpforeach パッケージ 2058 18 0 10
for を捨てよ、foreach を書こう 1817 19 0 13
データの不備を統計的に見抜く (Gelman’s secret weapon) 2661 14 0 12
カップルが一緒にお風呂に入る割合をベイズ推定してみた 48378 118 0 149
pipeR の使い方【ノーカット版】 2469 15 0 11
可視化で理解するマルコフ連鎖モンテカルロ法 15380 35 0 88
Bokete player の裏側 ~R で作る Web アプリケーション~ 4048 12 0 12
Stan で欠測データの相関係数を推定してみた 4600 17 0 23
チェビシェフの不等式 5154 10 0 20
swirl パッケージでインタラクティブ学習 2960 13 0 16
RPubs とその Bot たち 5067 12 0 8
5分でわかるベイズ確率 61582 139 0 321

おー、できた!

みなさんベイズが好きなんですねー。

Enjoy!

関連

36
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
41