0

I want to use R to crawl the news from url(http://www.foxnews.com/search-results/search?q="AlphaGo"&ss=fn&start=0). Here is my code:

url <- "http://api.foxnews.com/v1/content/search?q=%22AlphaGo%22&fields=date,description,title,url,image,type,taxonomy&section.path=fnc&start=0&callback=angular.callbacks._0&cb=2017719162"
html <- str_c(readLines(url,encoding = "UTF-8"),collapse = "")
content_fox <- RJSONIO:: fromJSON(html)

However, the json could not be understood as the error showed up :

Error in file(con, "r") : cannot open the connection

I notice that the json starts from angular.callbacks._0 , which I think might be the problem.

Any idea how to fix this?

exteral
  • 991
  • 2
  • 12
  • 33

1 Answers1

0

According to the answer in Parse JSONP with R, I ajusted my code with two new ones and it worked:

url <- "http://api.foxnews.com/v1/content/search?q=%22AlphaGo%22&fields=date,description,title,url,image,type,taxonomy&section.path=fnc&start=0&callback=angular.callbacks._0&cb=2017719162"
html <- str_c(readLines(url,encoding = "UTF-8"),collapse = "")
html <- sub('[^\\{]*', '', html) # remove function name and opening parenthesis
html <- sub('\\)$', '', html) # remove closing parenthesis
content_fox <- RJSONIO:: fromJSON(html)
exteral
  • 991
  • 2
  • 12
  • 33