SlideShare a Scribd company logo
1 of 38
網路爬蟲實作 –
用 R 語言打造自己的爬蟲程式
David Chiu
2016/03/24
關於我
 大數軟體有限公司創辦人
 前趨勢科技工程師
 ywchiu.com
 大數學堂
http://course.largitdata.com/
 粉絲頁
https://www.facebook.com/largitdata
 Machine Learning With R
Cookbook
https://www.packtpub.com/big-data-
and-business-intelligence/machine-
learning-r-cookbook
2
 所有課程補充資料、投影片皆位於
https://github.com/ywchiu/rcrawler
課程資料
3
用 R 語言打造自己的爬蟲程式
4
使用爬蟲技術進行數據蒐集
5
商品
監控
品牌
監控對手
監控
使用者
反應
請求 Request
回應 Response
網頁網頁連結器
Web Connector
資料剖析
Data Parser
資料中心
Data Center
網路爬蟲架構
6
 於網頁上點選右鍵 -> 檢查
使用開發人員工具
7
點選檢查
1. 選擇 Network 頁籤
2. 點選 Doc
3. 點選 new/
觀察HTTP 請求與返回內容
8
GET
內容寫在上頭
http://www.appledaily.com.tw/realtimenews/section/new/
什麼是GET?
9
 讓 R 可以像Python 的
BeautifulSoup 一樣可以使用
CSS Selector快速萃取、剖析
網頁元素
 可以使用Magrittr 套件的管道
(Pipeline) 傳遞資料
使用 rvest
10
Hadley Wickham
http://hadley.nz/
 在Console 下打入
install.packages("rvest")
 或使用Rstudio
安裝rvest 套件
11
 在Console 下打入
library(rvest)
 或使用Rstudio
於右上搜尋框打入rvest
勾選rvest 旁邊的框框
載入rvest 套件
12
newsurl <- "http://www.appledaily.com.tw/realtimenews/section/new/"
apple <- read_html(newsurl, encoding="UTF-8")
開始蒐集網頁資料
13
選取Request URL
apple %>% iconv(from='UTF-8', to='UTF-8')
檢視資料
14
Windows 平台需要透過iconv 轉換編碼
%>% : pipeline 做傳遞資料用
DOM Tree
15
<html>
<body>
<h1 id="title">Hello World</h1>
<a href="#" class="link">This is link1</a>
<a href="# link2" class="link">This is link2</a>
</body>
</html>
html
h1 a a
body
Document Object Model
觀察元素抓取位置
16
1. 點選元素觀測
2. 觀察元素所在位置 <li class=“rtddt…”>
3. 下方可以觀察標籤路徑
 右鍵點選選取元素 -> Copy -> Copy selector
使用開發人員工具複製DOM 元素路徑
17
#maincontent > div.thoracis > div.abdominis.rlby.clearmen > ul > li.rtddt.ccc
 https://chrome.google.com/webstore/detail/infolite/ipjbad
abbpedegielkhgpiekdlmfpgal
使用InfoLite
18
綠色: 目前選取得區塊
黃色: 符合樣式的區塊
紅色: 排除的曲塊
Clear旁邊的數字: 符合區塊的數目
rtddt <- apple %>% html_nodes('.rtddt')
rtddt[1] %>% iconv(from='UTF-8', to='UTF-8')
抓取清單列表
19
標題: h1
類別: h2
時間: time
time <- rtddt %>%
html_nodes('time') %>%
html_text() %>%
iconv(from='UTF-8', to='UTF-8')
title <- rtddt %>%
html_nodes('h1') %>%
html_text() %>%
iconv(from='UTF-8', to='UTF-8')
category <- rtddt %>%
html_nodes('h2') %>%
html_text() %>%
iconv(from='UTF-8', to='UTF-8')
解析資料內容
抓取時間、類別、標頭
20
domain <- "http://www.appledaily.com.tw"
url <- rtddt %>%
html_attr('href') %>%
iconv(from='UTF-8', to='UTF-8')
url <- gsub(domain, "", url)
url <- paste0(domain, url)
取得網頁連結
21
使用html_attr 抓取 href
將domain 名稱加到url 前
applenews <- data.frame(time=time, title= title,
category=category, url = url)
將資料合併為Data Frame
22
觀察內容元素所在
23
位在.trans中
applenews$url = as.character(applenews$url)
applenews$content <- sapply(applenews$url,
function(e){read_html(e) %>% html_node('.trans')
%>% html_text() %>% iconv(from='UTF-8',
to='UTF-8') } )
抓取內文
24
news[news$category=="生活",]
整理資料後做分析
25
library("jiebaR")
mixseg = worker()
segment(code= as.character(news$title[1]) , jiebar
= mixseg)
使用jiebaR 斷詞
[1] "Fed害ㄟ紐約原油跌破50美元(0)"
[1] "Fed" "害" "ㄟ" "紐約" "原油" "跌破" "50" "美元" "0"
26
word <- unlist(sapply(news$title, function(e)
segment(code= as.character(e) , jiebar = mixseg)))
tb <- table(word)
計算詞頻
27
library(wordcloud)
wordcloud(names(tb), tb, , min.freq = 1,
random.order = F, ordered.colors = F, colors =
rainbow(length(1:3)))
呈現文字雲
28
如何抓取留言資訊
29
如何取得Facebook 留言?
30
使用Graph API Explorer
31
輸入網址列找尋ID
32
擷取評論
33
利用文章ID/comments取得評論內容
新增一個App
34
取得APP ID 跟 Secret
35
install.packages("rjson")
library(rjso)
library(httr)
TOKEN = '<TOKEN>'
fburl <-
paste0('https://graph.facebook.com/v2.5/922912944487485/comments
?access_token=', TOKEN)
fbcontent <- GET(fburl)
fbjson <- fromJSON(content(fbcontent, 'text'))
使用httr 抓取FB 留言
36
 Website:
ywchiu.com
 Email:
david@largitdata.com
聯絡方式
37
38

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

20160324 big data workshop - 網路爬蟲實作 - 用 r 語言打造自己的爬蟲程式