── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.2 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.3 ✔ tibble 3.2.1
✔ lubridate 1.9.2 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rvest)
Attaching package: 'rvest'
The following object is masked from 'package:readr':
guess_encoding
library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
library(ggplot2)
Specify the url you will use and start working on it
#Using CSS selectors to scrape the rankings sectionrank_data_html <-html_nodes(webpage,'.text-primary')#Converting the ranking data to textrank_data <-html_text(rank_data_html)#Let's have a look at the rankingshead(rank_data)
[1] "1." "2." "3." "4." "5." "6."
#Data-Preprocessing: Converting rankings to numericalrank_data<-as.numeric(rank_data)#Let's have another look at the rankingshead(rank_data)
[1] 1 2 3 4 5 6
length(rank_data)
[1] 100
#Using CSS selectors to scrape the title sectiontitle_data_html <-html_nodes(webpage,'.lister-item-header a')#Converting the title data to texttitle_data <-html_text(title_data_html)#Let's have a look at the titlehead(title_data)
#Using CSS selectors to scrape the description sectiondescription_data_html <-html_nodes(webpage,'.ratings-bar+ .text-muted')#Converting the description data to textdescription_data <-html_text(description_data_html)description_data <-gsub("\n", "", description_data)#Let's have a look at the description datahead(description_data)
[1] "On Halloween night, Tara Heyes finds herself as the obsession of a sadistic murderer known as Art the Clown."
[2] "A secret government agency recruits some of the most dangerous incarcerated super-villains to form a defensive task force. Their first mission: save the world from the apocalypse."
[3] "In the 17th century, two Portuguese Jesuit priests travel to Japan in an attempt to locate their mentor, who is rumored to have committed apostasy, and to propagate Catholicism."
[4] "A deaf and mute writer who retreated into the woods to live a solitary life must fight for her life in silence when a masked killer appears at her window."
[5] "Ed and Lorraine Warren travel to North London to help a single mother raising four children alone in a house plagued by a supernatural spirit."
[6] "Three girls are kidnapped by a man with a diagnosed 23 distinct personalities. They must try to escape before the apparent emergence of a frightful new 24th."
length(description_data)
[1] 100
#Using CSS selectors to scrape the Movie runtime sectionruntime_data_html <-html_nodes(webpage,'.text-muted .runtime')#Converting the runtime data to textruntime_data <-html_text(runtime_data_html)#Let's have a look at the runtimehead(runtime_data)
runtime_data<-gsub(" min","",runtime_data)runtime_data<-as.numeric(runtime_data)#Let's have another look at the runtime datahead(runtime_data)
[1] 85 123 161 82 134 117
length(runtime_data)
[1] 100
Scrape for Movie Genre Information
#Using CSS selectors to scrape the Movie genre sectiongenre_data_html <-html_nodes(webpage,'.genre')#Converting the genre data to textgenre_data <-html_text(genre_data_html)#Let's have a look at the runtimehead(genre_data)
#Data-Preprocessing: removing \ngenre_data<-gsub("\n","",genre_data)#Data-Preprocessing: removing excess spacesgenre_data<-gsub(" ","",genre_data)#taking only the first genre of each moviegenre_data<-gsub(",.*","",genre_data)#Convering each genre from text to factorgenre_data<-as.factor(genre_data)#Let's have another look at the genre datahead(genre_data)
[1] Horror Action Drama Horror Horror Horror
9 Levels: Action Adventure Animation Biography Comedy Crime Drama ... Horror
length(genre_data)
[1] 100
Scrape for Movie Rating Information
#Using CSS selectors to scrape the IMDB rating sectionrating_data_html <-html_nodes(webpage,'.ratings-imdb-rating strong')#Converting the ratings data to textrating_data <-html_text(rating_data_html)#Let's have a look at the ratingshead(rating_data)
[1] "5.6" "5.9" "7.2" "6.6" "7.3" "7.3"
#Data-Preprocessing: converting ratings to numericalrating_data<-as.numeric(rating_data)#Let's have another look at the ratings datahead(rating_data)
[1] 5.6 5.9 7.2 6.6 7.3 7.3
length(rating_data)
[1] 100
Scrape for Voting Information
#Using CSS selectors to scrape the votes sectionvotes_data_html <-html_nodes(webpage,'.sort-num_votes-visible span:nth-child(2)')#Converting the votes data to textvotes_data <-html_text(votes_data_html)#Let's have a look at the votes datahead(votes_data)
#Data-Preprocessing: removing commasvotes_data<-gsub(",","",votes_data)#Data-Preprocessing: converting votes to numericalvotes_data<-as.numeric(votes_data)#Let's have another look at the votes datahead(votes_data)
[1] 47734 710230 119468 149269 292311 532913
length(votes_data)
[1] 100
Scrape for Movie Director Information
#Using CSS selectors to scrape the directors sectiondirectors_data_html <-html_nodes(webpage,'.text-muted+ p a:nth-child(1)')#Converting the directors data to textdirectors_data <-html_text(directors_data_html)#Let's have a look at the directors datahead(directors_data)
#Data-Preprocessing: converting directors data into factorsdirectors_data<-as.factor(directors_data)
Scrape for Movie Actor Information
#Using CSS selectors to scrape the actors sectionactors_data_html <-html_nodes(webpage,'.lister-item-content .ghost+ a')#Converting the gross actors data to textactors_data <-html_text(actors_data_html)#Let's have a look at the actors datahead(actors_data)
#Data-Preprocessing: converting actors data into factorsactors_data<-as.factor(actors_data)length(actors_data)
[1] 99
Find metascore data with missing values and replace with NAs
ratings_bar_data <-html_nodes(webpage,'.ratings-bar') %>%# scrape the ratings bar and convert to texthtml_text2()head(ratings_bar_data) # look at the ratings bar
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
21.00 47.00 62.00 60.36 72.50 99.00 5
Find the missing gross earnings
# scrape the votess bar and convert to textvotes_bar_data <-html_nodes(webpage,'.sort-num_votes-visible') %>%html_text2()head(votes_bar_data) # look at the votes bar data
'data.frame': 100 obs. of 10 variables:
$ Rank : num 1 2 3 4 5 6 7 8 9 10 ...
$ Title : chr "Terrifier" "Suicide Squad" "Silence" "Hush" ...
$ Description : chr "On Halloween night, Tara Heyes finds herself as the obsession of a sadistic murderer known as Art the Clown." "A secret government agency recruits some of the most dangerous incarcerated super-villains to form a defensive "| __truncated__ "In the 17th century, two Portuguese Jesuit priests travel to Japan in an attempt to locate their mentor, who is"| __truncated__ "A deaf and mute writer who retreated into the woods to live a solitary life must fight for her life in silence "| __truncated__ ...
$ Runtime : num 85 123 161 82 134 117 139 145 128 108 ...
$ Genre : Factor w/ 9 levels "Action","Adventure",..: 9 1 7 9 9 9 4 7 5 3 ...
$ Rating : num 5.6 5.9 7.2 6.6 7.3 7.3 8.1 8.1 8 7.1 ...
$ Director : Factor w/ 97 levels "Alessandro Carloni",..: 19 22 61 65 44 59 63 71 18 34 ...
$ Metascore : num NA 40 79 67 65 63 71 85 94 59 ...
$ Votes : num 47734 710230 119468 149269 292311 ...
$ Gross_Earning_in_Mil: num NA 325.1 7.1 NA 102.4 ...
Rank Title
1 3 Silence
Description
1 In the 17th century, two Portuguese Jesuit priests travel to Japan in an attempt to locate their mentor, who is rumored to have committed apostasy, and to propagate Catholicism.
Runtime Genre Rating Director Metascore Votes Gross_Earning_in_Mil
1 161 Drama 7.2 Martin Scorsese 79 119468 7.1
Question 1: Based on the above data, which movie from which Genre had the longest runtime?
A: “Silence”, that is a drama movie, had the longest runtime (161min).