IMDB = Internet Movie Data Base

IMDB.com is a renowned online database dedicated to cataloging information about movies, TV shows, and celebrities. Established in 1990, IMDb is now under the ownership of Amazon. The platform is highly regarded for its extensive offerings, encompassing film and TV show ratings, user reviews, trailers, detailed cast and crew information, as well as the latest industry news.

In this context, we will embark on scraping data related to horror movies from IMDb, utilizing the ‘rvest’ package in R.

Our goal is to extract information on horror films and the cast associated with them.

# Install Libraries and once installed comment out

#install.packages("tidyverse")
#install.packages("rvest")

Both tidyverse and rvest are powerful tools that can greatly assist in various aspects of data analysis and web scraping within the R programming environment.

Some conflicts will come up but will not stop progress..

# Load the libraries

library(tidyverse)
## ── 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.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── 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

Awesome code to save csv for specific movie

This code does the following:

Extracts movie names, cleans up genre and synopsis. Stores movie information in a list (movie_info_list). Specifies the movie you want to convert to a CSV file. Checks if the chosen movie exists in the list. If the movie is found, it creates a data frame for the chosen movie and saves it as a CSV file with a filename based on the movie name. Replace “Alien” with the movie name you want to convert to a CSV file. The information for that movie will be saved in a CSV file with a filename like “Alien_info.csv.”

# Extract movie names from the web scraping
name <- page %>% html_nodes(".lister-item-header a") %>% html_text()

# Replace '\n' characters in 'genre' and 'synopsis'
genre <- page %>% html_nodes(".genre") %>% html_text()
genre <- gsub("\n", " ", genre)

synopsis <- page %>% html_nodes(".ratings-bar+ .text-muted") %>% html_text()
synopsis <- gsub("\n", " ", synopsis)

# Initialize an empty list to store movie information
movie_info_list <- list()

# Loop through the extracted movie names and extract information
for (movie_name in name) {
  movie_data <- movies[movies$name == movie_name, ]

  # Store the movie information in the list
  movie_info_list[[movie_name]] <- movie_data
}

# Specify the movie you want to convert to CSV (e.g., "Alien")
chosen_movie <- "Alien"

# Check if the chosen movie exists in the list
if (chosen_movie %in% names(movie_info_list)) {
  # Create a data frame with the information for the chosen movie
  chosen_movie_data <- movie_info_list[[chosen_movie]]
  
  # Define the filename for the CSV
  csv_filename <- paste0(chosen_movie, "_info.csv")
  
  # Write the chosen movie's information to a CSV file
  write.csv(chosen_movie_data, csv_filename, row.names = FALSE)
  
  cat("The information for '", chosen_movie, "' has been saved to '", csv_filename, "'.\n")
} else {
  cat("Movie not found in the list.")
}
## The information for ' Alien ' has been saved to ' Alien_info.csv '.

Convert all data from genre into .csv

The code goes through multiple website pages, extracting movie data for each page.

The “get_cast” function finds cast members’ names by taking movie links, reading the HTML, and using CSS selectors.

Store all this in the “horror-movies” data frame.

A loop navigates through website pages, creating URLs, reading HTML, and extracting data.

Use “sapply” to get cast information for each movie link.

Once all data is collected, the “movies” data frame is filled, and the loop moves to the next page.

You can view the scraped data and save it as a CSV file.

This code helps you gather movie data from IMDb for analysis or other purposes.

# Extract movie names from the web scraping
name <- page %>% html_nodes(".lister-item-header a") %>% html_text()

# Replace '\n' characters in 'genre' and 'synopsis'
genre <- page %>% html_nodes(".genre") %>% html_text()
genre <- gsub("\n", " ", genre)

synopsis <- page %>% html_nodes(".ratings-bar+ .text-muted") %>% html_text()
synopsis <- gsub("\n", " ", synopsis)

# Initialize an empty list to store movie information
movie_info_list <- list()

# Loop through the extracted movie names and extract information
for (movie_name in name) {
  movie_data <- movies[movies$name == movie_name, ]

  # Store the movie information in the list
  movie_info_list[[movie_name]] <- movie_data
}

# Create a data frame with information for all movies
all_movies_data <- do.call(rbind, movie_info_list)

# Define the filename for the CSV
csv_filename <- "all_horror_movies_info.csv"

# Write information for all movies to a CSV file
write.csv(all_movies_data, csv_filename, row.names = FALSE)

cat("The information for all horror movies has been saved to '", csv_filename, "'.\n")
## The information for all horror movies has been saved to ' all_horror_movies_info.csv '.