[英]create tibble in R
我想創建一個 tibble,其中包含這些刮掉的電影信息,這些信息按他們的等級排序。 列名分別為Rank、Poster、Movie、Year、Rating。 網頁鏈接是https://www.imdb.com/search/title/?title_type=feature&num_votes=25000,&genres=horror&view=simple&sort=num_votes,desc我的代碼是
library(rvest)
library(tidyverse)
link <- "https://www.imdb.com/search/title/?title_type=feature&num_votes=25000,&genres=horror&sort=user_rating,desc&view=simple&sort=user_rating"
horror <- read_html(link)
top50_horror <- horror %>%
tibble(x = 50, y = 5,
.name_repair = ~ c("Rank", "Poster", "Movie", "Year", "Rating"))
top50_horror
我可以從 web 中獲得一些幫助來創建他的 tibble,列名分別是排名、海報、電影、年份、評級,排名列也是整數。
使用選擇器小工具,您可以找出特定數據所在的類,分別提取它們並將它們放在一個 tibble 中。
library(rvest)
url <- 'https://www.imdb.com/search/title/?title_type=feature&num_votes=25000,&genres=horror&view=simple&sort=num_votes,desc'
webpage <- url %>% read_html()
webpage %>%
html_nodes('div.col-title span span a') %>%
html_text() -> title
webpage %>%
html_nodes('div.col-imdb-rating strong') %>%
html_text() %>%
trimws() %>%
as.numeric() -> ratings
data <- tibble::tibble(rank = seq_along(title), title, ratings)
data
# rank title ratings
# <int> <chr> <dbl>
# 1 1 The Shining 8.4
# 2 2 Alien 8.4
# 3 3 Psycho 8.5
# 4 4 World War Z 7
# 5 5 Zombieland 7.6
# 6 6 Shaun of the Dead 7.9
# 7 7 Get Out 7.7
# 8 8 It 7.3
# 9 9 The Conjuring 7.5
#10 10 Split 7.3
# … with 40 more rows
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.