LoginSignup
2
1

More than 3 years have passed since last update.

ブラウザをコマンドで開く【R言語】

Last updated at Posted at 2020-01-16

Rを使って自動でブラウザを起動

browseURL関数を使うとブラウザで指定のURLを開ける。

browseURL("https://www.r-project.org")

ブラウザに使うアプリを指定したい時は、

browseURL("https://www.r-project.org",
browser = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"
)

自動で開けると連続でダウンロードができる。

例えば

https://www.shinko-keirin.co.jp/keirinkan/kosu/mathematics/math_index.html

こんなありがたい勉強元サイトがあるけれど、問題のダウンロードが大変!!
一つ目と二つ目のURLを開いてみてURLを見てみると

https://www.shinko-keirin.co.jp/・・・/drill01.doc
https://www.shinko-keirin.co.jp/・・・/drill02.doc

末尾がdril〇〇になっていて、ここを変えれば自動でDLできそう。
合計で25講座あるので、

for(i in 1:25){
  if(i <= 9){
    URL <- paste0("https://www.shinko-keirin.co.jp/keirinkan/kosu/mathematics/word/worddrill/drill0",i,".doc")
  }else{
    URL <- paste0("https://www.shinko-keirin.co.jp/keirinkan/kosu/mathematics/word/worddrill/drill",i,".doc")
  }
  options(browser = "C:\\Program Files\\Mozilla Firefox\\firefox.exe")
  browseURL(URL)
  Sys.sleep(5)
}

これで連続自動ダウンロードできる。

実行時は、規約を読んでください。もしくはサイト運営者へ問い合わせてください。
サイトに負荷がかからないようsleep関数で遅延を挟むこと。

2
1
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
2
1