언어/JAVA

[Spring] Jsoup을 이용한 웹 크롤링

chan10 2022. 5. 1. 18:56

- Jsoup을 이용한 정적 크롤링

 

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
package com.fastcampus.ch4.crawling;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
 
import java.io.IOException;
 
@Controller
public class CrawlingTest {
    @GetMapping("/crawling")
    public String crawling() throws IOException {
        String URL = "https://okky.kr/articles/questions";
        Document doc;
        try {
            doc = Jsoup.connect(URL).get();
            Elements boards =doc.select("h5 > a");
 
            System.out.println("boards count : " + boards.size());
            for(int i=0; i < boards.size(); i++){
                String review = boards.get(i).text();
                System.out.printf("[%d] : %s \n", i+1, review);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "crawling";
    }
}