-
Notifications
You must be signed in to change notification settings - Fork 0
/
AS08_Web-Scraping-HTML.Rmd
120 lines (91 loc) · 4.36 KB
/
AS08_Web-Scraping-HTML.Rmd
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
---
title: "AS08_Web-Scraping-HTML"
author: "你是誰 R10342000 新聞所碩五"
date: "2022/05/19"
output:
html_document:
number_sections: no
theme: united
highlight: tango
toc: yes
toc_depth: 4
toc_float:
collapsed: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, results = 'markup', comment = '#>', error = TRUE)
```
### 題目說明
這份作業希望能夠讓你熟悉 Web Scraping 的流程,這週的重點會著重在 html。
## A. scrape appledaily foundation(50分)
本小題的案例為[蘋果基金會的捐款平台](https://tw.feature.appledaily.com/charity/projlist/1),請抓取捐款案的最近 10 頁資料,並整理出 2 個 dataframe,且分為以下欄位:
- df_case_list,包含捐款案id(case_id)、新聞標題(title)、捐款案時間(date)、捐款案狀態(status)、捐款總額(amount)、新聞連結(link)、捐款明細連結(link_detail)
- df_case_donation,包含捐款案id(case_id)、捐款明細連結(link_detail)、捐款者(donator)、捐款金額(donation)、捐款時間(donate_date)
另外,請注意以下幾點
- 請去掉下方程式碼的註解(檢查部分),以驗證你所抓到的資料未有重複
- 用橫向 bar chart 印出捐款次數最多的前十大單位或個體(注意,個體可能是以全形或半形逗號分隔。先不處理填錯名字的問題、也暫時不考慮如「XXX全家」的問題)
- 請用 `glimpse()` 分別呈現上述 tibble 的長相
### 作答區 - 爬蟲程式碼
你可以把結果匯出成 csv,這樣就不用每次 knit 都要重抓一次資料,不過爬蟲的 code 要留著喔!加上 # comment 就好。
```{r message=FALSE, warning=FALSE}
### your code
library(tidyverse)
# df_case_list %>% write_csv("data/AS08/df_case_list.csv")
# df_case_donation %>% write_csv("data/AS08/df_case_donation.csv")
```
### 作答區 - 作業要求檢查
這邊的 code 請去掉 comment 後執行喔!可以用來確認結果!
```{r message=FALSE, warning=FALSE}
### your code
# df_case_list <- read_csv("data/AS08/df_case_list.csv")
# df_case_donation <- read_csv("data/AS08/df_case_donation.csv")
#
# # 檢查部分!!! 請去掉!!!
# df_case_list %>% summarise(n_distinct(case_id))
# df_case_list %>% summarise(n_distinct(link))
# df_case_list %>% summarise(n_distinct(link_detail))
#
# df_case_donation %>% summarise(n_distinct(link_detail))
#
# # bar chart
#
# # 看長相
# df_case_list %>% glimpse()
# df_case_donation %>% glimpse()
```
## B. scrape PTT(50分)
本小題的案例為[PTT 感情版](https://www.ptt.cc/bbs/Boy-Girl/index.html),請抓取最近 5 頁的文章列表(20篇/頁),再抓取每篇文章的內文與留言,並整理出 3 個 dataframe,且分為以下欄位:
- df_index,包含作者(index_author)、標題(index_title)、連結(index_link)
- df_article,包含作者(article_author)、標題(article_title)、連結(index_link)、時間(article_dt)、內文(article_text)、IP與國家(article_IP)、留言數(article_ncomments)
- df_comment,包含推文作者(comment_author)、推文時間(comment_dt)、推文內容(comment_text)、推文推噓(comment_type)、連結(index_link)
另外,請注意以下幾點
- 請去掉下方程式碼的註解(檢查部分),以驗證你所抓到的資料沒有重複
- 請用 `glimpse()` 分別呈現上述 tibble 的長相
- 請把文章(df_article)和留言(df_comment)串在一起
### 作答區 - 爬蟲程式碼
你可以把結果匯出成 csv,這樣就不用每次 knit 都要重抓一次資料,不過爬蟲的 code 要留著喔!加上 # comment 就好。
```{r message=FALSE, warning=FALSE}
### your code
library(tidyverse)
# df_index %>% write_csv("data/AS08/df_index.csv")
# df_article %>% write_csv("data/AS08/df_article.csv")
# df_comment %>% write_csv("data/AS08/df_comment.csv")
```
### 作答區 - 作業要求檢查
```{r message=FALSE, warning=FALSE}
### your code
# df_index <- read_csv("data/AS08/df_index.csv")
# df_article <- read_csv("data/AS08/df_article.csv")
# df_comment <- read_csv("data/AS08/df_comment.csv")
#
# # 檢查部分!!! 請去掉!!!
# df_index %>% summarise(n_distinct(index_link))
# df_article %>% summarise(n_distinct(index_link))
# df_comment %>% summarise(n_distinct(index_link))
#
# # 看長相
# df_index %>% glimpse()
# df_article %>% glimpse()
# df_comment %>% glimpse()
### your code - 串在一起
```