LoginSignup
10
9

More than 5 years have passed since last update.

Rvest(とstringr)で自衛隊日報を一括ダウンロードしてみる

Last updated at Posted at 2018-04-16

面白いと評判?の自衛隊日報をダウンロードするためのやっつけコードです、動かなかったらすいません
普段RばっかつかってるのでなんでRやねんってとこは勘弁してください


#########必要なパッケージをロードする########
#インストールされてなかったら
#install.packages(c("rvest","stringr")) 
#を実行

library(rvest)
library(stringr)

#リンクの取得(朝日新聞サイトを利用)
links <- read_html("https://www.asahi.com/articles/ASL4J669JL4JUEHF016.html") %>% 
  html_nodes("a") %>% html_attr("href") %>% str_subset(".pdf") %>% unique()

#ファイル名(リンクのうしろ10文字で"日付.pdf")
filenames <- links %>% str_sub(-10)

#保存するディレクトリをつくる
dir.create("nippo")

#ダウンロード繰り返し
for(i in 1:length(links)){
download.file(url = links[i], 
              destfile =  str_c("nippo/", filenames[i]), 
              method = "libcurl"
              ) #method指定いらないかも
Sys.sleep(1) #1秒あける
}

楽しく読みましょう

10
9
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
10
9