SlideShare a Scribd company logo
Ruby로 HTML parsing 하기
(nokogiri)
김용성
2015. 06. 25.
https://rubygems.org/gems/nokogiri
nokogiri ==
Nokogiri (鋸) is an HTML, XML, SAX, and Reader
parser. Among Nokogiri's many features is the
ability to search documents via XPath or CSS3
selectors. XML is like violence - if it doesn’t solve
your problems, you are not using enough of it.
Nokogiri 는 HTML, XML 파서 입니다.
Nokogiri는 XPath 또는 CSS3 셀렉터를 이용해
문서를 탐색하는 기능이 있습니다.
sudo apt-get install zlib1g-dev
sudo gem install nokogiri
설치(on ubuntu)
Other OS : http://www.nokogiri.org/tutorials/
installing_nokogiri.html
예제
<html>
<body>
<table border=1>
<tr>
<td>월</td>
<td>화</td>
<td>수</td>
</tr>
<tr>
<td>야근</td>
<td>칼퇴</td>
<td>조퇴</td>
</tr>
</table>
</body>
</html>
예제
<html>
<body>
<table border=1>
<tr>
<td>월</td>
<td>화</td>
<td>수</td>
</tr>
<tr>
<td>야근</td>
<td>칼퇴</td>
<td>조퇴</td>
</tr>
</table>
</body>
</html>
얘만 빼보자.
예제
require ‘nokogiri’
html_doc = Nokogiri::HTML(“<html><body><table border=1><tr><td>월</td><td>
화</td><td>수</td></tr><tr><td>야근</td><td>칼퇴</td><td>조퇴</td></tr></
table></body></html>”)
result = html_doc.xpath(‘//html/body/table/tr/td') // 이렇게 xpath를 이용해 접근
puts result[4].content //html 코드중 4번째 td태그의 값을 출력함
nokogiri는 웹 크롤링 할때 편하다
ruby 프로젝트에서 웹크롤링을 해서 DB에 값을 저장할때 쓰면 편함
url을 파싱할수도 있고, xml도 파싱 가능함

More Related Content

Viewers also liked

Корпус миру в Україні. Марат КЮРЧЕВСЬКИЙ
Корпус миру в Україні. Марат КЮРЧЕВСЬКИЙКорпус миру в Україні. Марат КЮРЧЕВСЬКИЙ
Корпус миру в Україні. Марат КЮРЧЕВСЬКИЙ
Anastasiia Popsuy
 
Resp 2590 ct scans (1)
Resp 2590 ct scans (1)Resp 2590 ct scans (1)
Resp 2590 ct scans (1)
kjkuzmich
 
Ppp_vet_atyrau_rus_03.04.14
Ppp_vet_atyrau_rus_03.04.14Ppp_vet_atyrau_rus_03.04.14
Ppp_vet_atyrau_rus_03.04.14
Asilbek Zhakiyev
 
Zulhafiz bin zolkepeli 4 m3k2
Zulhafiz bin zolkepeli 4 m3k2Zulhafiz bin zolkepeli 4 m3k2
Zulhafiz bin zolkepeli 4 m3k2
Sarah Cloud
 
20131115_atyrau_6thwg_presentation_rus_RPPAtyrau
20131115_atyrau_6thwg_presentation_rus_RPPAtyrau20131115_atyrau_6thwg_presentation_rus_RPPAtyrau
20131115_atyrau_6thwg_presentation_rus_RPPAtyrau
Asilbek Zhakiyev
 

Viewers also liked (20)

SBIproducciones - SBI GROUP MEXICO
SBIproducciones - SBI GROUP MEXICOSBIproducciones - SBI GROUP MEXICO
SBIproducciones - SBI GROUP MEXICO
 
Look at the man in the mirror - the smart way for change management in perso...
Look at the man in the mirror  - the smart way for change management in perso...Look at the man in the mirror  - the smart way for change management in perso...
Look at the man in the mirror - the smart way for change management in perso...
 
파이썬 xml 이해하기
파이썬 xml 이해하기파이썬 xml 이해하기
파이썬 xml 이해하기
 
Smart Mirror for Digital Signage
Smart Mirror for Digital SignageSmart Mirror for Digital Signage
Smart Mirror for Digital Signage
 
Tema 2
Tema 2Tema 2
Tema 2
 
Skateboarding
SkateboardingSkateboarding
Skateboarding
 
Demanda y comportamiento del consumidor
Demanda y comportamiento del consumidorDemanda y comportamiento del consumidor
Demanda y comportamiento del consumidor
 
Many electrons atoms_2012.12.04 (PDF with links
Many electrons atoms_2012.12.04 (PDF with linksMany electrons atoms_2012.12.04 (PDF with links
Many electrons atoms_2012.12.04 (PDF with links
 
digestive
digestivedigestive
digestive
 
Корпус миру в Україні. Марат КЮРЧЕВСЬКИЙ
Корпус миру в Україні. Марат КЮРЧЕВСЬКИЙКорпус миру в Україні. Марат КЮРЧЕВСЬКИЙ
Корпус миру в Україні. Марат КЮРЧЕВСЬКИЙ
 
State of utah veterans benefits
State of utah veterans benefitsState of utah veterans benefits
State of utah veterans benefits
 
Class Show
Class ShowClass Show
Class Show
 
Nicolas Morin -- Kanban - The (non)recipe for success -- Lean Kanban France 2...
Nicolas Morin -- Kanban - The (non)recipe for success -- Lean Kanban France 2...Nicolas Morin -- Kanban - The (non)recipe for success -- Lean Kanban France 2...
Nicolas Morin -- Kanban - The (non)recipe for success -- Lean Kanban France 2...
 
Resp 2590 ct scans (1)
Resp 2590 ct scans (1)Resp 2590 ct scans (1)
Resp 2590 ct scans (1)
 
Ppp_vet_atyrau_rus_03.04.14
Ppp_vet_atyrau_rus_03.04.14Ppp_vet_atyrau_rus_03.04.14
Ppp_vet_atyrau_rus_03.04.14
 
ITS World Congress :: Vienna, Oct 2012
ITS World Congress :: Vienna, Oct 2012ITS World Congress :: Vienna, Oct 2012
ITS World Congress :: Vienna, Oct 2012
 
Digestive system
Digestive systemDigestive system
Digestive system
 
Zulhafiz bin zolkepeli 4 m3k2
Zulhafiz bin zolkepeli 4 m3k2Zulhafiz bin zolkepeli 4 m3k2
Zulhafiz bin zolkepeli 4 m3k2
 
20131115_atyrau_6thwg_presentation_rus_RPPAtyrau
20131115_atyrau_6thwg_presentation_rus_RPPAtyrau20131115_atyrau_6thwg_presentation_rus_RPPAtyrau
20131115_atyrau_6thwg_presentation_rus_RPPAtyrau
 
Climate advocacy emails content analysis ICA 2015 San Juan PR
Climate advocacy emails content analysis ICA 2015 San Juan PRClimate advocacy emails content analysis ICA 2015 San Juan PR
Climate advocacy emails content analysis ICA 2015 San Juan PR
 

Similar to Ruby html parsing

회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy
Jc Kim
 

Similar to Ruby html parsing (15)

처음배우는 자바스크립트, 제이쿼리 #1
처음배우는 자바스크립트, 제이쿼리 #1처음배우는 자바스크립트, 제이쿼리 #1
처음배우는 자바스크립트, 제이쿼리 #1
 
회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy
 
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modificationSecrets of the JavaScript Ninja - Chapter 12. DOM modification
Secrets of the JavaScript Ninja - Chapter 12. DOM modification
 
스타트업사례로 본 로그 데이터분석 : Tajo on AWS
스타트업사례로 본 로그 데이터분석 : Tajo on AWS스타트업사례로 본 로그 데이터분석 : Tajo on AWS
스타트업사례로 본 로그 데이터분석 : Tajo on AWS
 
Hacosa jquery 1th
Hacosa jquery 1thHacosa jquery 1th
Hacosa jquery 1th
 
Partner Story(Megazone): 금융사 실전 프로젝트 DeepDive
Partner Story(Megazone): 금융사 실전 프로젝트 DeepDive Partner Story(Megazone): 금융사 실전 프로젝트 DeepDive
Partner Story(Megazone): 금융사 실전 프로젝트 DeepDive
 
스타트업 사례로 본 로그 데이터 분석 : Tajo on AWS
스타트업 사례로 본 로그 데이터 분석 : Tajo on AWS스타트업 사례로 본 로그 데이터 분석 : Tajo on AWS
스타트업 사례로 본 로그 데이터 분석 : Tajo on AWS
 
Big data analysis with R and Apache Tajo (in Korean)
Big data analysis with R and Apache Tajo (in Korean)Big data analysis with R and Apache Tajo (in Korean)
Big data analysis with R and Apache Tajo (in Korean)
 
JavaSript Template Engine
JavaSript Template EngineJavaSript Template Engine
JavaSript Template Engine
 
자바 웹 개발 시작하기 (9주차 : 프로젝트 구현 – 추가적인 뷰)
자바 웹 개발 시작하기 (9주차 : 프로젝트 구현 – 추가적인 뷰)자바 웹 개발 시작하기 (9주차 : 프로젝트 구현 – 추가적인 뷰)
자바 웹 개발 시작하기 (9주차 : 프로젝트 구현 – 추가적인 뷰)
 
웹표준 교육
웹표준 교육웹표준 교육
웹표준 교육
 
[1B6]Realm a database for android & ios
[1B6]Realm a database for android & ios[1B6]Realm a database for android & ios
[1B6]Realm a database for android & ios
 
Polymer Codelab: Before diving into polymer
Polymer Codelab: Before diving into polymerPolymer Codelab: Before diving into polymer
Polymer Codelab: Before diving into polymer
 
[122]네이버의모던웹라이브러리 박재성
[122]네이버의모던웹라이브러리 박재성[122]네이버의모던웹라이브러리 박재성
[122]네이버의모던웹라이브러리 박재성
 
[week14] Getting started with D3.js
[week14] Getting started with D3.js[week14] Getting started with D3.js
[week14] Getting started with D3.js
 

Ruby html parsing