在R中执行curl语句以下载JSON文件

时间:2015-10-17 13:49:01

标签: json r curl

是否可以使用RCurl或其他包在R中执行此curl语句,以便返回JSON文件?

curl "http://toerisme.groningen.nl/over-groningen/groningen-op-de-kaart/zoeken?q=+Zoeken+op+woord"%"2C+bedrijfsnaam+of+plaats+&plaats=&branches"%"5B"%"5D=238" \
     -H "Host: toerisme.groningen.nl" \
     -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0" \
     -H "Accept: text/javascript, text/html, application/xml, text/xml, */*" \
     -H "Accept-Language: nl,en-US;q=0.7,en;q=0.3" \
     -H "X-Requested-With: XMLHttpRequest" \
     -H "X-Prototype-Version: 1.7.1" \
     -H "Referer: http://toerisme.groningen.nl/over-groningen/groningen-op-de-kaart" \
     -H "Cookie: _ga=GA1.2.1354184564.1444979582; _gat=1" \
     -H "Connection: keep-alive"\
     --compressed

我已经尝试了

test <- getURL(url,  
               cookie = "_ga=GA1.2.1354184564.1444979582; _gat=1", 
               referer = "http://toerisme.groningen.nl/over-groningen/groningen-op-de-kaart", 
               useragent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0", 
               followlocation = TRUE)

但它并没有返回我需要的JSON文件。

1 个答案:

答案 0 :(得分:0)

结果很简单:

    library(httr)
    r <- POST('http://toerisme.groningen.nl/over-groningen/groningen-op-de-kaart/zoeken?q=+Zoeken+op+woord,+bedrijfsnaam+of+plaats+&plaats=&branches[]=238',
      add_headers(`X-Requested-With` = "XMLHttpRequest"))
    stop_for_status(r)
    x <- content(r, "parsed", "application/json")