Tutorial Web Scraping: Menggabungkan RSelenium dan Rvest
Sudah sering saya membahas bagaimana melakukan web scraping dengan
cara parsing html
menggunakan
R. Namun teknik ini tidak
selalu bisa diandalkan untuk mengambil data dari situs yang dinamis.
Berdasarkan pengalaman saya, teknik web scraping yang paling handal
adalah dengan mimicking browser seperti menggunakan
chromote
. Namun
permasalahan teknik ini adalah kerumitannya dalam hal penulisan skrip
untuk scraping suatu data tertentu dari tampilan webpage.
Pertanyaannya:
Lantas, apakah ada cara web scraping yang handal (memanfaatkan teknik
mimicking browser) sekaligus mudah penulisan skripnya (seperti
parsing html
)?
Jawabannya: ADA.
Kita bisa mengawinkan keduanya memanfaatkan dua libraries berikut ini:
RSelenium
dan rvest
.
RSelenium
RSelenium
adalah salah satu library mimicking browser terbaik yang
ada di R. Kita bisa memanfaatkan RSelenium
untuk membuka suatu
webpage dinamis.
rvest
Kemudian webpage tersebut bisa kita parsing menggunakan rvest
.
Sangat mudah.
Contoh Penggunaan
Sebagai contoh, saya akan melakukan web scraping tabel data dinamis dari webpage Dapodik.
Ada dua langkah yang saya lakukan:
- Membuka situs Dapodik menggunakan
RSelenium
. - Parsing
RSelenium
denganrvest
.
Berikut skripnya:
# load libraries
library(dplyr)
library(rvest)
library(RSelenium)
# ===============================================================================
# memulai selenium
driver = RSelenium::rsDriver(browser = "chrome",
chromever = "99.0.4844.35" )
remote_driver = driver[["client"]]
# url
url = "https://dapo.kemdikbud.go.id/sp"
# membuka url dengan selenium
remote_driver$navigate(url)
# parsing html dengan membaca selenium menggunakan rvest
df =
remote_driver$getPageSource()[[1]] %>%
read_html() %>%
html_table(fill = T)
</break> </break> </break>
Berikut adalah hasil data mentahnya:
## [[1]]
## # A tibble: 37 x 38
## No Wilayah Total Total Total TK TK TK KB KB KB TPA
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 No Wilayah Jml N S Jml N S Jml N S Jml
## 2 1 Prov. Jaw… 67.352 19.8… 47.5… 18.6… 179 18.4… 14.7… 3 14.7… 398
## 3 2 Prov. Jaw… 62.069 20.4… 41.6… 9.454 156 9.298 13.5… 4 13.5… 139
## 4 3 Prov. Jaw… 53.540 20.1… 33.3… 14.1… 176 13.9… 10.2… 12 10.2… 587
## 5 4 Prov. Sum… 23.815 10.6… 13.1… 3.084 206 2.878 5.180 148 5.032 59
## 6 5 Prov. Sul… 16.555 8.178 8.377 4.617 272 4.345 1.823 3 1.820 76
## 7 6 Prov. Ban… 14.678 4.848 9.830 2.537 107 2.430 3.477 2 3.475 28
## 8 7 Prov. Nus… 13.949 5.646 8.303 1.632 262 1.370 3.746 57 3.689 24
## 9 8 Prov. Lam… 13.348 5.516 7.832 3.125 108 3.017 2.450 0 2.450 41
## 10 9 Prov. Sum… 12.998 5.784 7.214 2.300 135 2.165 3.151 4 3.147 24
## # … with 27 more rows, and 26 more variables: TPA <chr>, TPA.1 <chr>,
## # SPS <chr>, SPS.1 <chr>, SPS.2 <chr>, PKBM <chr>, PKBM.1 <chr>,
## # PKBM.2 <chr>, SKB <chr>, SKB.1 <chr>, SKB.2 <chr>, SD <chr>, SD.1 <chr>,
## # SD.2 <chr>, SMP <chr>, SMP.1 <chr>, SMP.2 <chr>, SMA <chr>, SMA.1 <chr>,
## # SMA.2 <chr>, SMK <chr>, SMK.1 <chr>, SMK.2 <chr>, SLB <chr>, SLB.1 <chr>,
## # SLB.2 <chr>
</break> </break> </break>
Berikut adalah datanya setelah dibersihkan:
No | Wilayah | Total_Jml | Total_N | Total_S | TK_Jml | TK_N | TK_S | KB_Jml | KB_N | KB_S | TPA_Jml | TPA_N | TPA_S | SPS_Jml | SPS_N | SPS_S | PKBM_Jml | PKBM_N | PKBM_S | SKB_Jml | SKB_N | SKB_S | SD_Jml | SD_N | SD_S | SMP_Jml | SMP_N | SMP_S | SMA_Jml | SMA_N | SMA_S | SMK_Jml | SMK_N | SMK_S | SLB_Jml | SLB_N | SLB_S |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Prov. Jawa Timur | 67.352 | 19.844 | 47.508 | 18.663 | 179 | 18.484 | 14.742 | 3 | 14.739 | 398 | 0 | 398 | 4.487 | 1 | 4.486 | 936 | 0 | 936 | 20 | 20 | 0 | 19.110 | 17.119 | 1.991 | 4.921 | 1.729 | 3.192 | 1.523 | 423 | 1.100 | 2.148 | 298 | 1.850 | 404 | 72 | 332 |
2 | Prov. Jawa Barat | 62.069 | 20.403 | 41.666 | 9.454 | 156 | 9.298 | 13.580 | 4 | 13.576 | 139 | 0 | 139 | 6.508 | 1 | 6.507 | 1.891 | 0 | 1.891 | 26 | 26 | 0 | 19.695 | 17.384 | 2.311 | 5.810 | 1.983 | 3.827 | 1.686 | 513 | 1.173 | 2.897 | 288 | 2.609 | 383 | 48 | 335 |
3 | Prov. Jawa Tengah | 53.540 | 20.160 | 33.380 | 14.161 | 176 | 13.985 | 10.218 | 12 | 10.206 | 587 | 0 | 587 | 2.967 | 1 | 2.966 | 730 | 1 | 729 | 34 | 34 | 0 | 18.796 | 17.529 | 1.267 | 3.415 | 1.770 | 1.645 | 852 | 360 | 492 | 1.591 | 238 | 1.353 | 189 | 39 | 150 |
4 | Prov. Sumatera Utara | 23.815 | 10.649 | 13.166 | 3.084 | 206 | 2.878 | 5.180 | 148 | 5.032 | 59 | 0 | 59 | 392 | 1 | 391 | 488 | 0 | 488 | 22 | 22 | 0 | 9.778 | 8.214 | 1.564 | 2.693 | 1.333 | 1.360 | 1.081 | 427 | 654 | 979 | 269 | 710 | 59 | 29 | 30 |
5 | Prov. Sulawesi Selatan | 16.555 | 8.178 | 8.377 | 4.617 | 272 | 4.345 | 1.823 | 3 | 1.820 | 76 | 0 | 76 | 306 | 1 | 305 | 458 | 0 | 458 | 25 | 25 | 0 | 6.426 | 6.081 | 345 | 1.709 | 1.268 | 441 | 601 | 336 | 265 | 422 | 169 | 253 | 92 | 23 | 69 |
6 | Prov. Banten | 14.678 | 4.848 | 9.830 | 2.537 | 107 | 2.430 | 3.477 | 2 | 3.475 | 28 | 0 | 28 | 587 | 0 | 587 | 378 | 0 | 378 | 7 | 7 | 0 | 4.633 | 3.897 | 736 | 1.582 | 574 | 1.008 | 594 | 161 | 433 | 749 | 91 | 658 | 106 | 9 | 97 |
7 | Prov. Nusa Tenggara Timur | 13.949 | 5.646 | 8.303 | 1.632 | 262 | 1.370 | 3.746 | 57 | 3.689 | 24 | 1 | 23 | 333 | 2 | 331 | 251 | 2 | 249 | 20 | 20 | 0 | 5.195 | 3.372 | 1.823 | 1.798 | 1.360 | 438 | 591 | 378 | 213 | 316 | 161 | 155 | 43 | 31 | 12 |
8 | Prov. Lampung | 13.348 | 5.516 | 7.832 | 3.125 | 108 | 3.017 | 2.450 | 0 | 2.450 | 41 | 0 | 41 | 226 | 0 | 226 | 281 | 0 | 281 | 13 | 13 | 0 | 4.734 | 4.323 | 411 | 1.437 | 709 | 728 | 519 | 239 | 280 | 492 | 111 | 381 | 30 | 13 | 17 |
9 | Prov. Sumatera Selatan | 12.998 | 5.784 | 7.214 | 2.300 | 135 | 2.165 | 3.151 | 4 | 3.147 | 24 | 0 | 24 | 136 | 0 | 136 | 295 | 0 | 295 | 13 | 13 | 0 | 4.700 | 4.264 | 436 | 1.410 | 904 | 506 | 615 | 329 | 286 | 316 | 120 | 196 | 38 | 15 | 23 |
10 | Prov. Sumatera Barat | 11.074 | 5.180 | 5.894 | 2.545 | 110 | 2.435 | 1.828 | 9 | 1.819 | 106 | 2 | 104 | 485 | 1 | 484 | 253 | 0 | 253 | 20 | 20 | 0 | 4.269 | 3.983 | 286 | 857 | 676 | 181 | 334 | 236 | 98 | 214 | 114 | 100 | 163 | 29 | 134 |
11 | Prov. Aceh | 11.045 | 5.353 | 5.692 | 2.749 | 514 | 2.235 | 2.117 | 4 | 2.113 | 120 | 0 | 120 | 131 | 0 | 131 | 306 | 0 | 306 | 23 | 23 | 0 | 3.523 | 3.336 | 187 | 1.246 | 899 | 347 | 539 | 396 | 143 | 225 | 153 | 72 | 66 | 28 | 38 |
12 | Prov. Riau | 10.880 | 4.671 | 6.209 | 2.475 | 135 | 2.340 | 2.083 | 4 | 2.079 | 147 | 2 | 145 | 125 | 1 | 124 | 197 | 0 | 197 | 9 | 9 | 0 | 3.780 | 3.213 | 567 | 1.251 | 859 | 392 | 459 | 303 | 156 | 307 | 128 | 179 | 47 | 17 | 30 |
13 | Prov. Nusa Tenggara Barat | 10.467 | 4.082 | 6.385 | 2.084 | 183 | 1.901 | 2.654 | 0 | 2.654 | 33 | 0 | 33 | 207 | 0 | 207 | 441 | 0 | 441 | 10 | 10 | 0 | 3.292 | 3.010 | 282 | 1.011 | 608 | 403 | 341 | 154 | 187 | 343 | 99 | 244 | 51 | 18 | 33 |
14 | Prov. Kalimantan Barat | 9.780 | 5.713 | 4.067 | 911 | 151 | 760 | 2.068 | 6 | 2.062 | 37 | 0 | 37 | 74 | 1 | 73 | 188 | 0 | 188 | 9 | 9 | 0 | 4.438 | 4.139 | 299 | 1.354 | 1.018 | 336 | 451 | 267 | 184 | 226 | 108 | 118 | 24 | 14 | 10 |
15 | Prov. D.K.I. Jakarta | 8.818 | 2.009 | 6.809 | 1.942 | 77 | 1.865 | 390 | 23 | 367 | 33 | 22 | 11 | 1.692 | 43 | 1.649 | 283 | 39 | 244 | 0 | 0 | 0 | 2.240 | 1.309 | 931 | 1.079 | 293 | 786 | 494 | 117 | 377 | 576 | 73 | 503 | 89 | 13 | 76 |
16 | Prov. Kalimantan Selatan | 8.170 | 3.678 | 4.492 | 2.509 | 130 | 2.379 | 1.358 | 14 | 1.344 | 147 | 4 | 143 | 51 | 1 | 50 | 173 | 0 | 173 | 13 | 13 | 0 | 2.941 | 2.768 | 173 | 622 | 522 | 100 | 204 | 142 | 62 | 127 | 63 | 64 | 25 | 21 | 4 |
17 | Prov. D.I. Yogyakarta | 7.929 | 1.831 | 6.098 | 2.153 | 51 | 2.102 | 1.504 | 0 | 1.504 | 206 | 0 | 206 | 1.178 | 8 | 1.170 | 117 | 0 | 117 | 5 | 5 | 0 | 1.848 | 1.425 | 423 | 453 | 214 | 239 | 169 | 69 | 100 | 217 | 50 | 167 | 79 | 9 | 70 |
18 | Prov. Sulawesi Tengah | 7.869 | 3.855 | 4.014 | 2.061 | 135 | 1.926 | 1.237 | 3 | 1.234 | 15 | 0 | 15 | 106 | 2 | 104 | 201 | 1 | 200 | 15 | 15 | 0 | 2.922 | 2.671 | 251 | 868 | 731 | 137 | 230 | 175 | 55 | 183 | 106 | 77 | 31 | 16 | 15 |
19 | Prov. Jambi | 7.378 | 3.263 | 4.115 | 1.430 | 92 | 1.338 | 1.898 | 9 | 1.889 | 60 | 0 | 60 | 188 | 0 | 188 | 195 | 0 | 195 | 10 | 10 | 0 | 2.465 | 2.315 | 150 | 697 | 558 | 139 | 237 | 162 | 75 | 179 | 104 | 75 | 19 | 13 | 6 |
20 | Prov. Kalimantan Tengah | 7.006 | 3.555 | 3.451 | 1.998 | 122 | 1.876 | 692 | 0 | 692 | 64 | 0 | 64 | 212 | 1 | 211 | 118 | 1 | 117 | 9 | 9 | 0 | 2.652 | 2.419 | 233 | 855 | 708 | 147 | 243 | 181 | 62 | 139 | 95 | 44 | 24 | 19 | 5 |
21 | Prov. Sulawesi Tenggara | 6.637 | 3.558 | 3.079 | 2.052 | 221 | 1.831 | 535 | 1 | 534 | 10 | 0 | 10 | 17 | 0 | 17 | 325 | 2 | 323 | 21 | 21 | 0 | 2.340 | 2.255 | 85 | 777 | 692 | 85 | 310 | 246 | 64 | 171 | 103 | 68 | 79 | 17 | 62 |
22 | Prov. Sulawesi Utara | 6.177 | 2.195 | 3.982 | 1.735 | 105 | 1.630 | 790 | 3 | 787 | 13 | 0 | 13 | 104 | 1 | 103 | 103 | 0 | 103 | 29 | 29 | 0 | 2.219 | 1.363 | 856 | 729 | 475 | 254 | 230 | 121 | 109 | 191 | 92 | 99 | 34 | 6 | 28 |
23 | Prov. Kalimantan Timur | 6.038 | 2.452 | 3.586 | 1.455 | 94 | 1.361 | 1.149 | 3 | 1.146 | 84 | 0 | 84 | 136 | 0 | 136 | 117 | 0 | 117 | 16 | 16 | 0 | 1.914 | 1.651 | 263 | 679 | 448 | 231 | 230 | 142 | 88 | 221 | 87 | 134 | 37 | 11 | 26 |
24 | Prov. Papua | 5.936 | 2.652 | 3.284 | 825 | 154 | 671 | 658 | 56 | 602 | 13 | 5 | 8 | 191 | 7 | 184 | 464 | 25 | 439 | 12 | 11 | 1 | 2.629 | 1.648 | 981 | 737 | 514 | 223 | 253 | 144 | 109 | 144 | 81 | 63 | 10 | 7 | 3 |
25 | Prov. Bali | 5.415 | 2.839 | 2.576 | 1.565 | 129 | 1.436 | 508 | 2 | 506 | 58 | 0 | 58 | 22 | 0 | 22 | 70 | 0 | 70 | 9 | 9 | 0 | 2.422 | 2.274 | 148 | 410 | 272 | 138 | 164 | 87 | 77 | 173 | 54 | 119 | 14 | 12 | 2 |
26 | Prov. Maluku | 4.969 | 2.261 | 2.708 | 913 | 132 | 781 | 928 | 5 | 923 | 2 | 0 | 2 | 100 | 1 | 99 | 108 | 0 | 108 | 8 | 8 | 0 | 1.812 | 1.274 | 538 | 684 | 538 | 146 | 283 | 210 | 73 | 115 | 82 | 33 | 16 | 11 | 5 |
27 | Prov. Bengkulu | 4.395 | 1.983 | 2.412 | 1.141 | 97 | 1.044 | 803 | 2 | 801 | 101 | 1 | 100 | 86 | 0 | 86 | 130 | 0 | 130 | 10 | 10 | 0 | 1.407 | 1.304 | 103 | 447 | 381 | 66 | 145 | 109 | 36 | 107 | 64 | 43 | 18 | 15 | 3 |
28 | Prov. Sulawesi Barat | 3.847 | 1.862 | 1.985 | 805 | 80 | 725 | 870 | 8 | 862 | 19 | 0 | 19 | 23 | 1 | 22 | 173 | 1 | 172 | 6 | 6 | 0 | 1.326 | 1.298 | 28 | 379 | 318 | 61 | 90 | 77 | 13 | 131 | 60 | 71 | 25 | 13 | 12 |
29 | Prov. Maluku Utara | 3.777 | 1.870 | 1.907 | 699 | 127 | 572 | 655 | 38 | 617 | 1 | 0 | 1 | 55 | 0 | 55 | 148 | 0 | 148 | 8 | 7 | 1 | 1.316 | 1.112 | 204 | 507 | 367 | 140 | 218 | 139 | 79 | 149 | 63 | 86 | 21 | 17 | 4 |
30 | Prov. Kepulauan Riau | 3.041 | 1.142 | 1.899 | 687 | 81 | 606 | 446 | 0 | 446 | 25 | 0 | 25 | 118 | 0 | 118 | 84 | 0 | 84 | 5 | 5 | 0 | 972 | 683 | 289 | 407 | 234 | 173 | 161 | 93 | 68 | 116 | 36 | 80 | 20 | 10 | 10 |
31 | Prov. Gorontalo | 3.015 | 1.471 | 1.544 | 802 | 138 | 664 | 667 | 4 | 663 | 8 | 0 | 8 | 56 | 0 | 56 | 69 | 1 | 68 | 11 | 11 | 0 | 928 | 896 | 32 | 338 | 312 | 26 | 70 | 61 | 9 | 58 | 40 | 18 | 8 | 8 | 0 |
32 | Prov. Papua Barat | 2.706 | 1.141 | 1.565 | 455 | 51 | 404 | 470 | 24 | 446 | 18 | 0 | 18 | 32 | 2 | 30 | 107 | 17 | 90 | 4 | 4 | 0 | 1.097 | 691 | 406 | 330 | 233 | 97 | 131 | 82 | 49 | 57 | 33 | 24 | 5 | 4 | 1 |
33 | Prov. Kepulauan Bangka Belitung | 2.135 | 1.100 | 1.035 | 380 | 83 | 297 | 394 | 2 | 392 | 49 | 0 | 49 | 63 | 0 | 63 | 53 | 0 | 53 | 5 | 5 | 0 | 829 | 760 | 69 | 225 | 162 | 63 | 69 | 45 | 24 | 59 | 36 | 23 | 9 | 7 | 2 |
34 | Prov. Kalimantan Utara | 1.446 | 688 | 758 | 185 | 23 | 162 | 404 | 0 | 404 | 20 | 0 | 20 | 11 | 0 | 11 | 45 | 0 | 45 | 4 | 4 | 0 | 483 | 436 | 47 | 189 | 155 | 34 | 67 | 44 | 23 | 32 | 21 | 11 | 6 | 5 | 1 |
35 | Luar Negeri | 226 | 156 | 70 | 9 | 7 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 13 | 4 | 9 | 0 | 0 | 0 | 129 | 87 | 42 | 61 | 46 | 15 | 13 | 11 | 2 | 1 | 1 | 0 | 0 | 0 | 0 |
Total | Total | 438.480 | 171.588 | 266.892 | 96.138 | 4.823 | 91.315 | 85.473 | 453 | 85.020 | 2.765 | 37 | 2.728 | 21.405 | 77 | 21.328 | 10.189 | 94 | 10.095 | 451 | 449 | 2 | 149.260 | 130.503 | 18.757 | 41.967 | 23.863 | 18.104 | 14.197 | 6.979 | 7.218 | 14.371 | 3.691 | 10.680 | 2.264 | 619 | 1.645 |