我在R Studio for OSX中运行以下代码并暂停,但不在RStudio for Windows中。是什么给了什么?
当我跑步时
mikebay_movies <- lapply(vrottenrate(mikebay_movies$Film), function(x) as.data.frame(t(x), stringsAsFactors = FALSE))
它返回
Error in file(con, "r") : cannot open the connection
完整代码
#Create a function that will download movie info
rottenrate <- function(movie){
require(RJSONIO)
link <- paste("http://www.omdbapi.com/?t=", movie, "&y=&plot=short&r=json&tomatoes=true", sep = "")
jsonData <- fromJSON(link)
return(jsonData)
}
vrottenrate <- Vectorize(rottenrate, "movie", SIMPLIFY = F)
#Puts in the data into a table
mikebay_movies <- html("http://en.wikipedia.org/wiki/Michael_Bay") %>%
html_nodes ('#mw-content-text > table:nth-child(42)') %>%
html_table (fill = T) %>%
as.data.frame
#Grab all of the info and put it into a dataframe
mikebay_movies <- lapply(vrottenrate(mikebay_movies$Film), function(x) as.data.frame(t(x), stringsAsFactors = FALSE))
mikebay_movies_dt <- rbindlist(mikebay_movies,fill=TRUE)
答案 0 :(得分:0)
这可能是一种更好的方式来实现这一切:
library(omdbapi)
library(dplyr)
library(rvest)
library(magrittr)
library(pbapply)
html("http://en.wikipedia.org/wiki/Michael_Bay") %>%
html_nodes('#mw-content-text > table:nth-child(42)') %>%
html_table(fill=TRUE) %>% # *don't* use "T" (it kills kittens)
extract2(1) %>% # html_table returns a list; this is [[1]]
filter(!is.na(Film)) %>% # your scraper code has a data frame with NAs. remove them.
rowwise() %>% # apply the following "do" row-wise
do(find_by_title(title=.$Film, type="movie",
year_of_release=.$Year,
include_tomatoes=TRUE)) -> mike_bay_movies
glimpse(mike_bay_movies)
## Observations: 12
## Variables:
## $ Title (chr) "Bad Boys", "The Rock", "Armageddon", "Pearl Harbor", "Bad Boys II", "The ...
## $ Year (chr) "1995", "1996", "1998", "2001", "2003", "2005", "2007", "2009", "2011", "2...
## $ Rated (chr) "R", "R", "PG-13", "PG-13", "R", "PG-13", "PG-13", "PG-13", "PG-13", "R", ...
## $ Released (date) 1995-04-07, 1996-06-07, 1998-07-01, 2001-05-25, 2003-07-18, 2005-07-22, 2...
## $ Runtime (chr) "118 min", "136 min", "151 min", "183 min", "147 min", "136 min", "144 min...
## $ Genre (chr) "Action, Comedy, Crime", "Action, Adventure, Thriller", "Action, Adventure...
## $ Director (chr) "Michael Bay", "Michael Bay", "Michael Bay", "Michael Bay", "Michael Bay",...
## $ Writer (chr) "George Gallo (story), Michael Barrie (screenplay), Jim Mulholland (screen...
## $ Actors (chr) "Lisa Boyle, Will Smith, Martin Lawrence, Michael Taliferro", "Sean Conner...
## $ Plot (chr) "Two hip detectives protect a murder witness while investigating a case of...
## $ Language (chr) "English", "English", "English, Russian, Indonesian", "English, Japanese, ...
## $ Country (chr) "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA...
## $ Awards (chr) "1 win & 3 nominations.", "Nominated for 1 Oscar. Another 8 wins & 8 nomin...
## $ Poster (chr) "http://ia.media-imdb.com/images/M/MV5BMTY4NDk1NTU5NF5BMl5BanBnXkFtZTcwMjE...
## $ Metascore (chr) "41", "59", "42", "44", "38", "50", "61", "35", "42", "45", "32", "N/A"
## $ imdbRating (dbl) 6.8, 7.4, 6.6, 6.0, 6.5, 6.9, 7.1, 6.0, 6.3, 6.5, 5.8, 8.0
## $ imdbVotes (dbl) 153208, 240659, 293093, 230508, 161893, 247077, 469215, 292303, 294781, 14...
## $ imdbID (chr) "tt0112442", "tt0117500", "tt0120591", "tt0213149", "tt0172156", "tt039920...
## $ Type (chr) "movie", "movie", "movie", "movie", "movie", "movie", "movie", "movie", "m...
## $ tomatoMeter (int) 43, 66, 39, 25, 23, 40, NA, 19, 35, 49, 18, NA
## $ tomatoImage (chr) "rotten", "fresh", "rotten", "rotten", "rotten", "rotten", "N/A", "rotten"...
## $ tomatoRating (dbl) 4.9, 6.6, 5.2, 4.6, 4.1, 5.4, NA, 3.9, 4.9, 5.4, 3.9, NA
## $ tomatoReviews (int) 47, 64, 115, 188, 176, 194, NA, 242, 246, 182, 180, NA
## $ tomatoFresh (int) 20, 42, 45, 47, 40, 77, NA, 47, 87, 90, 32, NA
## $ tomatoRotten (int) 27, 22, 70, 141, 136, 117, NA, 195, 159, 92, 148, NA
## $ tomatoConsensus (chr) "Bad Boys stars Will Smith and Martin Lawrence have enjoyable chemistry; u...
## $ tomatoUserMeter (dbl) 78, 86, 73, 67, 78, 64, 75, 58, 56, 47, 52, 85
## $ tomatoUserRating (dbl) 3.5, 3.6, 3.5, 3.3, 3.6, 3.3, 3.9, 3.5, 3.5, 3.1, 3.3, 4.0
## $ tomatoUserReviews (int) 659318, 303907, 907616, 903898, 500480, 371029, 232, 4078681, 255142, 8424...
## $ DVD (date) 2000-06-27, 1997-12-02, 1999-01-05, 2001-12-04, 2003-12-09, 2005-12-13, 2...
## $ BoxOffice (chr) "N/A", "N/A", "$201.6M", "$197.8M", "$138.4M", "$35.8M", "N/A", "$402.1M",...
## $ Production (chr) "Sony Pictures Home Entertainment", "Disney", "Buena Vista", "Touchstone",...
## $ Website (chr) "N/A", "N/A", "N/A", "http://studio.go.com/movies/pearlharbor/flash/index....
## $ Response (chr) "True", "True", "True", "True", "True", "True", "True", "True", "True", "T...