library(rvest)
library(robotstxt)
library(dplyr)
library(purrr)

Q5: Is scraping allowed?

url <- "https://en.wikipedia.org/wiki/2026_in_film"
paths_allowed(url)
##  en.wikipedia.org
## [1] TRUE

Q6: Highest-grossing films of 2026

page <- read_html(url)

hg_2026 <- page %>%
  html_element("table.wikitable") %>%
  html_table()

hg_2026
## # A tibble: 10 × 4
##     Rank Title                        Distributor           `Worldwide gross`
##    <int> <chr>                        <chr>                 <chr>            
##  1     1 Spider-Man: Brand New Day †  Sony                  $2,523,147,515   
##  2     2 The Odyssey                  Universal             $1,804,356,298   
##  3     3 Toy Story 5                  Disney                $1,149,474,196   
##  4     4 Michael                      Lionsgate / Universal $1,028,703,388   
##  5     5 The Super Mario Galaxy Movie Universal             $1,013,278,851   
##  6     6 The Devil Wears Prada 2      20th Century          $693,492,689     
##  7     7 Project Hail Mary            Amazon MGM / Sony     $685,040,805     
##  8     8 Pegasus 3                    Maoyan                $656,459,523     
##  9     9 Minions & Monsters           Universal             $530,319,698     
## 10    10 Obsession                    Focus / Universal     $524,535,537

Q8 (Bonus): Highest-grossing films, 2020 to 2025

scrape_hg <- function(yr) {
  Sys.sleep(1)
  paste0("https://en.wikipedia.org/wiki/", yr, "_in_film") %>%
    read_html() %>%
    html_element("table.wikitable") %>%
    html_table() %>%
    mutate(across(everything(), as.character)) %>%
    mutate(year = yr)
}

hg_films <- map_dfr(2020:2025, scrape_hg)

hg_films %>% count(year)
## # A tibble: 6 × 2
##    year     n
##   <int> <int>
## 1  2020    10
## 2  2021    10
## 3  2022    10
## 4  2023    10
## 5  2024    10
## 6  2025    10
head(hg_films, 15)
## # A tibble: 15 × 5
##    Rank  Title                               Distributor `Worldwide gross`  year
##    <chr> <chr>                               <chr>       <chr>             <int>
##  1 1     Demon Slayer: Kimetsu no Yaiba Mug… Toho / Ani… $507,127,293[4]    2020
##  2 2     The Eight Hundred                   CMC Pictur… $461,421,559       2020
##  3 3     My People, My Homeland              China Lion  $433,241,288[5]    2020
##  4 4     Bad Boys for Life                   Sony        $426,505,244       2020
##  5 5     Tenet                               Warner Bro… $365,309,519       2020
##  6 6     Sonic the Hedgehog                  Paramount   $319,715,683       2020
##  7 7     Dolittle                            Universal   $251,410,631       2020
##  8 8     Jiang Ziya                          Beijing En… $243,883,429       2020
##  9 9     A Little Red Flower                 HG Enterta… $238,600,000[6][…  2020
## 10 10    Shock Wave 2                        Universe F… $226,400,000       2020
## 11 1     Spider-Man: No Way Home             Sony        $1,921,426,073[n…  2021
## 12 2     The Battle at Lake Changjin         Bona        $909,596,236       2021
## 13 3     Hi, Mom                             Lian Ray    $841,674,419       2021
## 14 4     No Time to Die                      MGM / Univ… $774,253,007       2021
## 15 5     F9                                  Universal   $726,229,501       2021