Import Library

message('Loading Packages')
## Loading Packages
library(rvest)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()         masks stats::filter()
## ✖ readr::guess_encoding() masks rvest::guess_encoding()
## ✖ dplyr::lag()            masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(mongolite)
library(httr)

Introduction

IBL adalah singkatan dari “Indonesia Basketball League,” yang merupakan liga bola basket profesional di Indonesia. Liga ini dikelola oleh Perbasi (Persatuan Bola Basket Seluruh Indonesia) dan bertujuan untuk mempromosikan dan mengembangkan olahraga bola basket di Indonesia.

Hasil Scraping Data IBL

Klub Dewa United

url_dewaunited<- "https://iblindonesia.com/profile/team/126042?season=37811"
page_dewaunited <-read_html(url_dewaunited)

pemain_dewaunited <- page_dewaunited %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% 
  html_text()
# Pastikan hasil dalam bentuk vector
pemain_dewaunited_vector <- as.vector(pemain_dewaunited)


point_dewaunited <-page_dewaunited %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]') %>% 
  html_text()
point_dewaunited_vector <-as.vector(point_dewaunited)

assist_dewaunited <-page_dewaunited %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]') %>% 
  html_text()
assist_dewaunited_vector <-as.vector(assist_dewaunited)

rebound_dewaunited <-page_dewaunited %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]') %>% 
  html_text()
rebound_dewaunited_vector <-as.vector(rebound_dewaunited)

Klub Pelita Jaya

url_pelitajaya<-'https://iblindonesia.com/profile/team/126036?season=37811'
page_pelitajaya<-read_html(url_pelitajaya)

pemain_pelitajaya <- page_pelitajaya %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% 
  html_text()
pemain_pelitajaya_vector <- as.vector(pemain_pelitajaya)

point_pelitajaya <-page_pelitajaya %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]') %>% 
  html_text()
point_pelitajaya_vector <- as.vector(point_pelitajaya)

assist_pelitajaya <-page_pelitajaya %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]') %>% 
  html_text()
assist_pelitajaya_vector <- as.vector(assist_pelitajaya)

rebound_pelitajaya <-page_pelitajaya %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]') %>% 
  html_text()
rebound_pelitajaya_vector <- as.vector(rebound_pelitajaya)

Klub Kesatrya Solo

url_kesatriasolo<-'https://iblindonesia.com/profile/team/170783?season=37811'
page_kesatriasolo<-read_html(url_kesatriasolo)

pemain_kesatriasolo <- page_kesatriasolo %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% 
  html_text()
pemain_kesatriasolo_vector <- as.vector(pemain_kesatriasolo)

point_kesatriasolo <-page_kesatriasolo %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 2) and parent::*)]') %>% 
  html_text()
point_kesatriasolo_vector <- as.vector(point_kesatriasolo)

assist_kesatriasolo <-page_kesatriasolo %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 3) and parent::*)]') %>% 
  html_text()
assist_kesatriasolo_vector <- as.vector(assist_kesatriasolo)

rebound_kesatriasolo <-page_kesatriasolo %>% 
  html_nodes(xpath='//*[contains(concat( " ", @class, " " ), concat( " ", "scroll-x-cont", " " )) and (((count(preceding-sibling::*) + 1) = 2) and parent::*)]//td[(((count(preceding-sibling::*) + 1) = 5) and parent::*)]') %>% 
  html_text()
rebound_kesatriasolo_vector <- as.vector(rebound_kesatriasolo)
nama_pemain<-c(pemain_kesatriasolo,pemain_pelitajaya,pemain_dewaunited)
point_pemain<-c(point_kesatriasolo,point_pelitajaya,point_dewaunited)
assist_pemain<-c(assist_kesatriasolo,assist_pelitajaya,assist_dewaunited)
rebound_pemain<-c(rebound_kesatriasolo,rebound_pelitajaya,rebound_dewaunited)

Membuat Hasil Scraping dalam Data Frame

data_ibl<-data.frame(nama_pemain,point_pemain,assist_pemain,rebound_pemain,stringsAsFactors = FALSE)

Mengubah Setiap Variabel Menjadi Numerik

# Konversi semua kolom (kecuali 'nama_pemain') ke format numerik
data_ibl[, -1] <- lapply(data_ibl[, -1], as.numeric)

summary(data_ibl)
##  nama_pemain         point_pemain   assist_pemain    rebound_pemain  
##  Length:53          Min.   :  0.0   Min.   :  0.00   Min.   :  0.00  
##  Class :character   1st Qu.: 17.0   1st Qu.:  2.00   1st Qu.:  7.00  
##  Mode  :character   Median : 60.0   Median : 16.00   Median : 33.00  
##                     Mean   :105.4   Mean   : 27.87   Mean   : 49.19  
##                     3rd Qu.:145.0   3rd Qu.: 39.00   3rd Qu.: 57.00  
##                     Max.   :514.0   Max.   :168.00   Max.   :251.00
glimpse(data_ibl)
## Rows: 53
## Columns: 4
## $ nama_pemain    <chr> "Nuke Tri Saputra", "Tifan Eka Pradita", "Kevin Moses E…
## $ point_pemain   <dbl> 116, 10, 167, 56, 1, 62, 1, 47, 79, 2, 46, 67, 283, 0, …
## $ assist_pemain  <dbl> 34, 6, 30, 40, 4, 21, 0, 16, 32, 0, 5, 12, 77, 1, 115, …
## $ rebound_pemain <dbl> 54, 2, 33, 14, 7, 33, 0, 11, 34, 2, 19, 33, 197, 0, 251…

Visualisasi Data IBL

Top 5 Pemain Pencetak Point Terbanyak

# Sort data by point_pemain in descending order
sorted_data <- data_ibl[order(-data_ibl$point_pemain), ]

# Get top 5 players
top_5_scorers <- head(sorted_data, 5)

print(top_5_scorers)
##                         nama_pemain point_pemain assist_pemain rebound_pemain
## 15         Kentrell Debarus Barkley          514           115            251
## 50              Jordan Lavell Adams          513           103            149
## 39                   Lester Prosper          368            33            209
## 49     Gelvis Andres Solano Paulino          335           168             79
## 52 Tavario Earnest Ptristian Miller          291            39            191
ggplot(top_5_scorers, aes(x = reorder(nama_pemain, -point_pemain), y = point_pemain, fill = nama_pemain)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = point_pemain), vjust = -0.3) +
  theme_minimal() +
  labs(title = "Top 5 Pencetak Point",
       x = "Nama Pemain",
       y = "Point Pemain") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  guides(fill = FALSE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Top 5 Pemain Pencetak Rebound Terbanyak

df_sorted <- data_ibl[order(data_ibl$rebound_pemain, decreasing = TRUE), ]

# Mendapatkan 5 pemain dengan rebound terbanyak
top_5_rebounders <- head(df_sorted, 5)

# Menampilkan hasil
print(top_5_rebounders)
##                         nama_pemain point_pemain assist_pemain rebound_pemain
## 15         Kentrell Debarus Barkley          514           115            251
## 39                   Lester Prosper          368            33            209
## 13                     Taylor Johns          283            77            197
## 52 Tavario Earnest Ptristian Miller          291            39            191
## 34  Kevin Ornell Chapman MC Daniels          282            56            156
# Misalkan Anda memiliki data frame 'df' dengan kolom 'nama_pemain' dan 'rebound_pemain'
# Anda dapat mengganti 'df', 'nama_pemain', dan 'rebound_pemain' dengan nama data frame dan kolom yang
# Membuat plot
ggplot(top_5_rebounders, aes(x = reorder(nama_pemain, -rebound_pemain), y = rebound_pemain, fill = nama_pemain)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  labs(title = "Top 5 Pemain dengan Rebound Terbanyak",
       x = "Nama Pemain",
       y = "Rebound Pemain") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  guides(fill = FALSE)

Top 5 Pemain Pencetak Assist Terbanyak

df_sorted1 <- data_ibl[order(data_ibl$assist_pemain, decreasing = TRUE), ]

# Mendapatkan 5 pemain dengan rebound terbanyak
top_5_assist <- head(df_sorted1, 5)

# Menampilkan hasil
print(top_5_assist)
##                     nama_pemain point_pemain assist_pemain rebound_pemain
## 49 Gelvis Andres Solano Paulino          335           168             79
## 15     Kentrell Debarus Barkley          514           115            251
## 50          Jordan Lavell Adams          513           103            149
## 47          Hardianus Hardianus           60            81             34
## 13                 Taylor Johns          283            77            197
ggplot(top_5_assist, aes(x = reorder(nama_pemain, -assist_pemain), y = assist_pemain, fill = nama_pemain)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = assist_pemain), vjust = -0.3) +
  theme_minimal() +
  labs(title = "Top 5 Pemain Pencetak Assist Terbanyak",
       x = "Nama Pemain",
       y = "Assist Pemain") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  guides(fill = FALSE)


  1. Department Statistika dan Sains Data IPB, ↩︎