이미지 파일 및 설정 파일 삭제, 새로운 CLI 및 문서 추가

This commit is contained in:
2025-03-04 12:19:53 +09:00
parent fb66b69644
commit 2c0a2d738c
33 changed files with 3149 additions and 54 deletions

66
docs/21_웹 크롤러.md Normal file
View File

@@ -0,0 +1,66 @@
Ruby에서 간단한 **웹 크롤러**를 작성하려면 `nokogiri``open-uri` 라이브러리를 사용할 수 있다.
### **1. 사전 준비**
웹 크롤링을 위해 `nokogiri`를 설치해야 한다.
```sh
gem install nokogiri
```
---
### **2. 간단한 웹 크롤러 예제**
다음은 특정 웹사이트에서 제목을 가져오는 간단한 크롤러이다.
```ruby
require 'nokogiri'
require 'open-uri'
# 크롤링할 웹페이지 URL
url = "https://news.ycombinator.com/"
# 페이지 가져오기
html = URI.open(url)
doc = Nokogiri::HTML(html)
# 뉴스 제목 가져오기
doc.css('.titleline a').each_with_index do |title, index|
puts "#{index + 1}. #{title.text}"
end
```
---
### **3. 코드 설명**
1. **라이브러리 로드**
- `nokogiri` : HTML을 파싱하는 라이브러리
- `open-uri` : 웹페이지 내용을 가져오는 라이브러리
2. **웹페이지 가져오기**
```ruby
html = URI.open(url)
doc = Nokogiri::HTML(html)
```
- `URI.open(url)` : 해당 URL의 HTML을 가져온다.
- `Nokogiri::HTML(html)` : HTML을 파싱하여 문서 객체를 생성한다.
3. **데이터 추출**
```ruby
doc.css('.titleline a').each_with_index do |title, index|
puts "#{index + 1}. #{title.text}"
end
```
- `.css('.titleline a')` : CSS 선택자로 제목을 선택한다.
- `.each_with_index` : 제목을 하나씩 출력한다.
---
### **4. 실행 결과 예시**
```sh
1. OpenAI releases GPT-4.5
2. Ruby 4.0 announced
3. NASA discovers new exoplanet
...
```
이제 Ruby를 사용하여 원하는 웹페이지의 데이터를 쉽게 크롤링할 수 있다.