-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-rvest.qmd
414 lines (315 loc) · 8.79 KB
/
14-rvest.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
---
title: "`rvest`:<br>Web Scraping 101"
block-headings: TRUE
author: "<br/><br/><br/>Alfa Nugraha Pradana"
institute: "Prodi Statistika dan Sains Data IPB University"
footer: "[rpubs.com/alfanugraha/sta1562-p14](https://rpubs.com/alfanugraha/sta1562-p14) "
format:
revealjs:
theme: [default, style.scss]
slide-number: c/t
code-copy: true
# center-title-slide: false
code-overflow: wrap
highlight-style: a11y
height: 1080
width: 1920
logo: assets/img/LogoIPBUni.png
preview-links: auto
editor: source
---
## Outline
<br/>
- HTML
- CSS Selector
- Ekstraksi Data
- Atribut
- Tabel
- GitHub Actions
- YAML
## HTML
> singkatan dari HyperText Markup Language
<br>
```{{html}}
<html>
<head>
<title>Page title</title>
</head>
<body>
<h1 id='first'>A heading</h1>
<p>Some text & <b>some bold text.</b></p>
<img src='myimg.png' width='100' height='100'>
</body>
```
<br>
. . .
HTML memiliki struktur hirarki yang dengan disusun oleh berbagai macam **elemen** yang diawali dengan:
* tag pembuka (`<tag>`),
* atribut (`id="first"`),
* diakhir dengan tag penutup (`</tag>`),
* dan konten yang diisi di antara tag pembuka dan penutup
## Membaca HTML dengan `rvest`
<br>
Scraping proses diawal dengan `read_html()`. Nilai kembalian dari perintah tersebut adalah objek dalam bentuk dokumen XML yang nanti akan dimanipulasi dengan fungsi pada `rvest`
```{r}
#| eval: true
#| echo: true
library(rvest)
library(dplyr)
html <- read_html("http://rvest.tidyverse.org/")
class(html)
```
<br>
`rvest` juga menyediakan fungsi untuk membuat dokumen XML dari suatu HTML
```{r}
#| eval: true
#| echo: true
html <- minimal_html("
<p>This is a paragraph<p>
<ul>
<li>This is a bulleted list</li>
</ul>
")
html
```
## Selektor CSS
> CSS adalah singkatan dari Cascading Style Sheets yang digunakan sebagai alat untuk mendefinisikan tampilan visual pada dokumen HTML
CSS menyediakan bahasa miniatur untuk memilih elemen pada suatu halaman web yang dikenal dengan istilah CSS selectors. Selektor ini bisa saja kompleks tetapi akan digunakan secara sederhana pada `rvest`. Empat hal penting pada selektor:
. . .
* `p`: memilih semua elemen tag `<p>`
* `.title`: memilih semua elemen dengan *class* `title`
* `p.special`: memilih semua elemen tag `<p>` dengan *class* `special`
* `#title`: memilih elemen dengan atribut `id` yang berisi `title`. Atribut `id` harus unik di dalam suatu dokumen HTML, sehingga hanya akan ada satu elemen yang terpilih
::: footer
[CSS Dinner](https://flukeout.github.io/)
:::
##
```{r}
#| eval: true
#| echo: true
html <- minimal_html("
<h1>This is a heading</h1>
<p id='first'>This is a paragraph</p>
<p class='important'>This is an important paragraph</p>
")
```
```{r}
#| eval: true
#| echo: true
html %>% html_element("h1")
```
<br>
```{r}
#| eval: true
#| echo: true
html %>% html_elements("p")
```
<br>
```{r}
#| eval: true
#| echo: true
html %>% html_elements(".important")
```
<br>
```{r}
#| eval: true
#| echo: true
html %>% html_elements("#first")
```
## Ekstraksi Data
```{r}
#| eval: true
#| echo: true
html <- minimal_html("
<ol>
<li>apple & pear</li>
<li>banana</li>
<li>pineapple</li>
</ol>
")
html %>%
html_elements("li") %>%
html_text2()
```
<br>
```{r}
#| eval: true
#| echo: true
html <- minimal_html("<body>
<p>
This is
a
paragraph.</p><p>This is another paragraph.
It has two sentences.</p>
")
html %>%
html_element("body") %>%
html_text() %>%
cat()
```
## Atribut
<br>
Contoh atribut yang digunakan sebagai informasi destinasi suatu alamat dan gambar.
```{r}
#| eval: true
#| echo: true
html <- minimal_html("
<p><a href='https://en.wikipedia.org/wiki/Cat'>cats</a></p>
<img src='https://cataas.com/cat' width='100' height='200'>
")
html %>%
html_elements("a") %>%
html_attr("href")
html %>%
html_elements("img") %>%
html_attr("src")
```
## Tabel
```{r}
#| eval: true
#| echo: true
html <- minimal_html("
<table>
<tr>
<th>x</th>
<th>y</th>
</tr>
<tr>
<td>1.5</td>
<td>2.7</td>
</tr>
<tr>
<td>4.9</td>
<td>1.3</td>
</tr>
<tr>
<td>7.2</td>
<td>8.1</td>
</tr>
</table>
")
html %>%
html_node("table") %>%
html_table()
```
## `html_element` vs `html_elements`
```{r}
#| eval: true
#| echo: true
html <- minimal_html("
<ul>
<li><b>C-3PO</b> is a <i>droid</i> that weighs <span class='weight'>167 kg</span></li>
<li><b>R2-D2</b> is a <i>droid</i> that weighs <span class='weight'>96 kg</span></li>
<li><b>Yoda</b> weighs <span class='weight'>66 kg</span></li>
<li><b>R4-P17</b> is a <i>droid</i></li>
</ul>
")
```
```{r}
#| eval: true
#| echo: true
html %>% html_elements("b") %>% html_text2()
```
<br>
```{r}
#| eval: true
#| echo: true
characters <- html %>% html_elements("li")
characters %>% html_element("b") %>% html_text2()
```
<br>
```{r}
#| eval: true
#| echo: true
data.frame(
name = characters %>% html_element("b") %>% html_text2(),
species = characters %>% html_element("i") %>% html_text2(),
weight = characters %>% html_element(".weight") %>% html_text2()
)
```
## GitHub Actions
<br>
GitHub Actions adalah platform CI/CD (continuous integration and continuous delivery) yang memungkinkan pengguna mengotomatisasi pengembangan tools dengan membuat suatu `workflow`.
GitHub Actions menyediakan mesin virtual Linux, Windows, dan MacOS untuk mengeksekusi workflow tersebut.
![](https://docs.github.com/assets/cb-25535/mw-1440/images/help/actions/overview-actions-simple.webp)
::: footer
[GitHub Actions](https://docs.github.com/en/actions/quickstart)
:::
## YAML
```{[yml]}
name: Daily Scraping Covid
on:
schedule:
- cron: '*/7 * * * *' # every 7 minutes
jobs:
covid-scrape:
runs-on: macos-latest
env:
ATLAS_URL: ${{ secrets.ATLAS_URL }}
ATLAS_COLLECTION: ${{ secrets.ATLAS_COLLECTION }}
ATLAS_DB: ${{ secrets.ATLAS_DB }}
steps:
- name: Start time
run: echo "$(date) ** $(TZ=Asia/Jakarta date)"
- uses: actions/checkout@v3
- uses: r-lib/actions/setup-r@v2
- name: Install mongolite package
run: Rscript -e 'install.packages("mongolite", dependencies = TRUE)'
- name: Install rvest package
run: Rscript -e 'install.packages("rvest", dependencies = TRUE)'
- name: Scrape data
run: Rscript scrape.R
```
## `rvest` & `mongolite`
```{r}
#| eval: false
#| echo: true
library(rvest)
library(mongolite)
url <- "https://www.worldometers.info/coronavirus/country/indonesia/"
html <- read_html(url)
count <- html_text(html_nodes(html, ".maincounter-number"), trim=T)
atlas <- mongo(
collection = Sys.getenv("ATLAS_COLLECTION"),
db = Sys.getenv("ATLAS_DB"),
url = Sys.getenv("ATLAS_URL")
)
newcovid <- data.frame(no = atlas$count() + 1, cases = count[1], deaths = count[2], recovered = count[3])
atlas$insert(newcovid)
atlas$disconnect()
```
::: footer
[Mongo Covid](https://github.com/sta1562/mongo-covid/)
:::
## Langkah Membangun Scraping Bot {.scrollable}
::: {style="font-size: 0.7em;"}
* Pada GitHub, buat repositori projek
* Clone projek tersebut menjadi sesi projek baru pada RStudio
* Membuat sintaks R untuk proses scraping dan storing data ke MongoDB Atlas
* Membuat sintaks YAML untuk proses penjadwalan otomatis pada GitHub Actions
* Push seluruh script yang dibutuhkan
* Pada Database Deployment Atlas, pilih menu [*Connect* - *Driver*], copy string koneksi berikut
![](assets/img/atlas-conn.png){fig-align="center"}
* Kembali ke GitHub, Aktifkan Action permissions pada menu [*Actions* - *General* - *Allow all actions and reusable workflows*]
* Buat respository secret yang baru pada menu *Actions secrets and variabels*, dan isi dengan string yang sudah dipersiapkan sebelumnya untuk nama koleksi, database, dan Atlas URL
![](assets/img/action.png){fig-align="center" width=1000}
* Periksa secara berkala workflow yang sudah dibangun
![](assets/img/workflow.png){fig-align="center" width=1000}
* Buat status badge
![](assets/img/badge.png){fig-align="center" width=1000}
* Sematkan di dalam README.md projek untuk menampilkan status otomatisasi scraping bot yang sudah aktif
![](assets/img/badge2.png){fig-align="center" width=500}
* DONE
:::
## Tugas Akhir Praktikum MDS Sesi UAS
<br>
* Membuat scraping bot terjadwal pada situs web yang sudah dipilih sebelumnya menggunakan `rvest` dan GitHub Actions
* Menyimpan hasil scraping tersebut disimpan ke dalam MongoDB Cloud Atlas
* Membuat narasi tentang tugas yang dikerjakan pada akun GitHub
* (Opsional) Visualisasi hasil dalam bentuk tabel, grafik, laporan di [RPubs](https://rpubs.com), atau ShinyApps
* Kriteria penilaian:
1. Teknik scraping
2. Kompleksitas pengerjaan
3. (Bonus) Penyajian hasil akhir
* Projek akan dinilai pada hari Jumat, 16 Juni 2023
# Pertanyaan? {background="#43464B"}