⚙️ 개발/Java
jsoup
지구용사
2024. 3. 19. 14:11
jsop
HTML 또는 XML 작업을 단순화하는 Java 라이브러리이다.
정적페이지를 크롤링 하는데 주로 사용된다.
🔎 크롤링 (Crawling)
HTML 페이지를 가져와서 필요한 데이터를 추출하는 작업
라이브러리 추가
https://mvnrepository.com/artifact/org.jsoup/jsoup
Connection.Response
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.Test;
import java.io.IOException;
@Test
void jsoup() throws IOException {
Connection.Response response = Jsoup.connect("http://www.google.com")
.method(Connection.Method.GET)
.execute();
Document document = response.parse();
String html = document.html();
System.out.println(html);
String text = document.text();
System.out.println(text);
}
html은 해당 페이지의 문서 모두를 가져온다.
text는 html 태그 사이에 있는 문자열들을 가져온다.
Select
해당 페이지에서 일정 부분만 가져오고 싶은 경우
Connection.Response response = Jsoup.connect("http://www.google.com")
.method(Connection.Method.GET)
.execute();
Elements elements = response.parse().select(".gb_jd");
System.out.println(elements);
🔗 참고 링크