curl: A Modern and Flexible Web Client for R

モダンで柔軟なR用ウェブクライアント

> library(curl)

バージョン: 0.9.5


関数名 概略
curl Curl connection interface
curl_download Download file to disk
curl_escape URL encoding
curl_fetch_memory Fetch the contents of a URL
curl_version List curl version and options.
form_file Upload files with form
handle Create and configure a curl handle
handle_cookies Extract cookies from a handle
ie_proxy Internet Explorer proxy settings
parse_date Parse date/time
parse_headers Parse response headers

curl

Arguments

  • url
  • open
  • handle
> con <- curl("http://api.moemoe.tokyo/anime/v1/master/2016")
> readLines(con)
Warning in readLines(con): incomplete final line found on 'http://
api.moemoe.tokyo/anime/v1/master/2016'
[1] "[{\"id\":281,\"title\":\"機動戦士ガンダム サンダーボルト\"},{\"id\":282,\"title\":\"プリンス・オブ・ストライド オルタナティブ\"},{\"id\":283,\"title\":\"無彩限のファントム・ワールド\"},{\"id\":284,\"title\":\"ハルチカ〜ハルタとチカは青春する〜\"},{\"id\":285,\"title\":\"ノルン+ノネット\"},{\"id\":286,\"title\":\"アクティヴレイド -機動強襲室第八係-\"},{\"id\":287,\"title\":\"少女たちは荒野を目指す\"},{\"id\":288,\"title\":\"僕だけがいない街\"},{\"id\":289,\"title\":\"おじさんとマシュマロ\"},{\"id\":290,\"title\":\"ファンタシースターオンライン2 ジ アニメーション\"},{\"id\":291,\"title\":\"だがしかし\"},{\"id\":292,\"title\":\"暗殺教室(第2期)\"},{\"id\":293,\"title\":\"ディバインゲート\"},{\"id\":294,\"title\":\"おしえて!ギャル子ちゃん\"},{\"id\":295,\"title\":\"石膏ボーイズ\"},{\"id\":296,\"title\":\"霊剣山 星屑たちの宴\"},{\"id\":297,\"title\":\"GATE 自衛隊 彼の地にて、斯く戦えり(2期)\"},{\"id\":298,\"title\":\"昭和元禄落語心中\"},{\"id\":299,\"title\":\"紅殻のパンドラ\"},{\"id\":300,\"title\":\"ブブキ・ブランキ\"},{\"id\":301,\"title\":\"ラクエンロジック\"},{\"id\":302,\"title\":\"デュラララ!!×2 結\"},{\"id\":303,\"title\":\"ナースウィッチ小麦ちゃんR\"},{\"id\":304,\"title\":\"虹色デイズ\"},{\"id\":305,\"title\":\"大家さんは思春期!\"},{\"id\":306,\"title\":\"Dimension W\"},{\"id\":307,\"title\":\"灰と幻想のグリムガル\"},{\"id\":308,\"title\":\"シュヴァルツェスマーケン\"},{\"id\":309,\"title\":\"最弱無敗の神装機竜(バハムート)\"},{\"id\":310,\"title\":\"赤髪の白雪姫(第2期)\"},{\"id\":311,\"title\":\"てーきゅう(第7期)\"},{\"id\":312,\"title\":\"魔法少女なんてもういいですから。\"},{\"id\":313,\"title\":\"蒼の彼方のフォーリズム\"},{\"id\":314,\"title\":\"この素晴らしい世界に祝福を!\"},{\"id\":315,\"title\":\"亜人\"},{\"id\":316,\"title\":\"FAIRY TAIL ZERO\"},{\"id\":317,\"title\":\"ももくり\"},{\"id\":318,\"title\":\"この男子、魔法がお仕事です。\"},{\"id\":319,\"title\":\"SUSHI POLICE\"},{\"id\":320,\"title\":\"血液型くん!4\"}]"

curl_download

curlによるファイルのダウンロード

> curl_download(url = "http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip", destfile = tempfile())

curl_escape / curl_unescape

URLのエンコードとデコード

> (out <- curl_escape("foo = bar + 5"))
[1] "foo%20%3D%20bar%20%2B%205"
> curl_unescape(out)
[1] "foo = bar + 5"

curl_fetch_memory / curl_fetch_disk / curl_fetch_stream

Arguments

  • url
  • handle
  • path
  • fun
> res <- curl_fetch_memory("http://httpbin.org/cookies/set?foo=123&bar=ftw")
> res$content
 [1] 7b 0a 20 20 22 63 6f 6f 6b 69 65 73 22 3a 20 7b 0a 20 20 20 20 22 62
[24] 61 72 22 3a 20 22 66 74 77 22 2c 20 0a 20 20 20 20 22 66 6f 6f 22 3a
[47] 20 22 31 32 33 22 0a 20 20 7d 0a 7d 0a

curl_options / curl_version

> curl_options() %>% head()
 accept_encoding accepttimeout_ms    address_scope           append 
           10102              212              171               50 
     autoreferer       buffersize 
              58               98
> curl_version()
$version
[1] "7.43.0"

$ssl_version
[1] "SecureTransport"

$libz_version
[1] "1.2.5"

$host
[1] "x86_64-apple-darwin15.0"

$protocols
 [1] "dict"   "file"   "ftp"    "ftps"   "gopher" "http"   "https" 
 [8] "imap"   "imaps"  "ldap"   "ldaps"  "pop3"   "pop3s"  "rtsp"  
[15] "smb"    "smbs"   "smtp"   "smtps"  "telnet" "tftp"

handle / new_handle

handle_cookies

> h <- new_handle()
> req <- curl_fetch_memory("https://ask.fm", handle = h)
> handle_cookies(h)
            domain  flag path secure          expiration              name
1           ask.fm FALSE    /  FALSE 2017-02-05 03:00:51           country
2           ask.fm FALSE    /  FALSE 2017-02-05 03:00:51            locale
3 #HttpOnly_ask.fm FALSE    /  FALSE 2016-02-08 21:00:51 _m_ask_fm_session
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           value
1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             JP
2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             en
3 YklNRWN0Q08rMDZqYzVsWG5Pdm0ybHYydFV5ZU1IOENGRGk3RkpZTFlwRUtKZ1IyQkhha2lCRzFoV2I5dEdIVGVBQUt4M3IydWF2YmRQVFZwczUyU2VkeUxNaVZLYjQwOC82akt2akMrMjB0dVBLZXdPdkJkYmxJTU9XR3hEbytqU29icDdlRVU5SDNueXd3QXRocmN1WXNLSE05VnJKZ2l1QStJSjJ5L3E2RnZpNDVjK2UwdnQ0VllIVkdZTGM1R1E1c2VId1lpZjlLU1hRbGZNMlFGclY1MThpUFJKbWVGZFZ3RWp2WjcwcjFPaTdDa3NXb2txa2cxTkZwTTV1NElXTjlYSWpRZisvK0p3ZFdQcFdZTi9KOWgvYjNqQ2MrU1hVbkNtU2dGblAzTCtvd0Jxb1hTNU5VVStlRWhvM05RaWl0YTBnRUlWRlFBMSt4NzAvOFFBPT0tLUsyMmxER0pwd2ppYWNKT3BRZWhGRWc9PQ%3D%3D--81fe92dbc09d753bf21c2a0fe070b53f11ea1880

parse_date

日付の判読

> parse_date("Sunday, 06-Nov-94 08:49:37 GMT")
[1] "1994-11-06 17:49:37 JST"
> parse_date("06 Nov 1994 08:49:37")
[1] "1994-11-06 17:49:37 JST"
> parse_date("20040911 +0200")
[1] "2004-09-11 07:00:00 JST"

parse_headers

> req <- curl_fetch_memory("https://ask.fm")
> parse_headers(req$headers)
 [1] "HTTP/1.1 200 OK"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 [2] "Content-Type: text/html; charset=utf-8"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
 [3] "Transfer-Encoding: chunked"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
 [4] "Status: 200 OK"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
 [5] "Cache-Control: max-age=0, private, must-revalidate"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
 [6] "ETag: W/\"125f59c32976ba1b8d753969cb1988fe\""                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
 [7] "X-Frame-Options: SAMEORIGIN"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
 [8] "X-XSS-Protection: 1; mode=block"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 [9] "X-Content-Type-Options: nosniff"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
[10] "X-Request-Id: cd6bd76b-39d9-4f6e-9f38-db610d24878d"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
[11] "Date: Fri, 05 Feb 2016 12:00:52 GMT"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[12] "Set-Cookie: country=JP; path=/; expires=Sat, 04 Feb 2017 18:00:52 -0000"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[13] "Set-Cookie: locale=en; path=/; expires=Sat, 04 Feb 2017 18:00:52 -0000"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
[14] "Set-Cookie: _m_ask_fm_session=cEF2ZVlCRDJYcHY3N1F5UTcvYnpTZzFSWTJJWEd0ckp6Wm94bmt5QTg4RTRlbjVuQUlaM3BacHVWazlKbEp0TmdiMU5XN3ZRR2NsQSs4Nk9tTFA2QUM5WUtFeThxSzh5TmNEYkhvSmUvWFRhR2l6SEJzSHZDdkZuTEFkbGdCQW1WMHArRXR0OVNUbWlSL1IvSlZra1NwUkpTd1BwQnFzMlE3dVZ3dG1ldUdzV2huYjdTQTRITHpCZVZCWjMyNjN4UkIwOFJEaTNySU0xS21FaWVTWnAwaHBacWx4SjQ2cnI2NHpLM29aTzJDRG5rUEYwNmtrUVJjTXNSL0pEeXRTWFgwakVNK29oTHpoVkxXaVZ5US9wd1grS3VnKzBpTTM0OEl4cVl2aHJHYkxpYWZZb05BYUphV3BUZXEvTGxaaUZ3b09TRDhidW9DRUozRDFYVmtXUHNBPT0tLUtUbGd2ZWJzSDhiWm9abXQrMmZ5MHc9PQ%3D%3D--b3e9813e9e72abe658dcae1d8fd430401bce2997; path=/; expires=Mon, 08 Feb 2016 12:00:52 -0000; HttpOnly"
[15] "Server: Ask.FM Web Service"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[16] "Content-Encoding: gzip"
> parse_headers(req$headers, multiple = TRUE)
[[1]]
 [1] "HTTP/1.1 200 OK"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 [2] "Content-Type: text/html; charset=utf-8"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
 [3] "Transfer-Encoding: chunked"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
 [4] "Status: 200 OK"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
 [5] "Cache-Control: max-age=0, private, must-revalidate"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
 [6] "ETag: W/\"125f59c32976ba1b8d753969cb1988fe\""                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
 [7] "X-Frame-Options: SAMEORIGIN"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
 [8] "X-XSS-Protection: 1; mode=block"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 [9] "X-Content-Type-Options: nosniff"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
[10] "X-Request-Id: cd6bd76b-39d9-4f6e-9f38-db610d24878d"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
[11] "Date: Fri, 05 Feb 2016 12:00:52 GMT"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[12] "Set-Cookie: country=JP; path=/; expires=Sat, 04 Feb 2017 18:00:52 -0000"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[13] "Set-Cookie: locale=en; path=/; expires=Sat, 04 Feb 2017 18:00:52 -0000"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
[14] "Set-Cookie: _m_ask_fm_session=cEF2ZVlCRDJYcHY3N1F5UTcvYnpTZzFSWTJJWEd0ckp6Wm94bmt5QTg4RTRlbjVuQUlaM3BacHVWazlKbEp0TmdiMU5XN3ZRR2NsQSs4Nk9tTFA2QUM5WUtFeThxSzh5TmNEYkhvSmUvWFRhR2l6SEJzSHZDdkZuTEFkbGdCQW1WMHArRXR0OVNUbWlSL1IvSlZra1NwUkpTd1BwQnFzMlE3dVZ3dG1ldUdzV2huYjdTQTRITHpCZVZCWjMyNjN4UkIwOFJEaTNySU0xS21FaWVTWnAwaHBacWx4SjQ2cnI2NHpLM29aTzJDRG5rUEYwNmtrUVJjTXNSL0pEeXRTWFgwakVNK29oTHpoVkxXaVZ5US9wd1grS3VnKzBpTTM0OEl4cVl2aHJHYkxpYWZZb05BYUphV3BUZXEvTGxaaUZ3b09TRDhidW9DRUozRDFYVmtXUHNBPT0tLUtUbGd2ZWJzSDhiWm9abXQrMmZ5MHc9PQ%3D%3D--b3e9813e9e72abe658dcae1d8fd430401bce2997; path=/; expires=Mon, 08 Feb 2016 12:00:52 -0000; HttpOnly"
[15] "Server: Ask.FM Web Service"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[16] "Content-Encoding: gzip"