๐ ๏ธ ๊ฐ๋ฐ ํ๊ฒฝ
๐ Spring : Spring Boot 3.1.3
๐ ๏ธ Java : Amazon corretto 17
๐ ๏ธ ๊ตฌํ
Jsoup ์ ์ฉ
์ฐ์ Jsoup
๊ณต์ ๋ฌธ์์ ์ ํ ๊ธ์ ํ์ธํด๋ณด์!
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors.
Jsoup์ด๋, ์ค์ธ๊ณ HTML๊ณผ ์ฐ๋ํ๊ธฐ ์ํ ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, HTML5 DOM method์ CSS selector ๋ฑ์ ์ฌ์ฉํ์ฌ URL ๊ฐ์ ธ์ค๊ธฐ์ ๋ฐ์ดํฐ ์ถ์ถ ๋ฐ ์กฐ์์ ๋งค์ฐ ํธ๋ฆฌํ API๋ฅผ ์ ๊ณตํฉ๋๋ค.
์๊ฐ์ ๊ฐ์ด ํน์ URL
์ ์๋ HTML
๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ด๋ค.
Spring
์์ ์ด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์๋์ ๊ฐ์ด build.gradle
์ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
# build.gradle
implementation 'org.jsoup:jsoup:1.15.3'
maven
์ฌ์ฉ์๋ ์๋์ ๊ฐ์ด ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
# pom.xml
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
ํฌ๋กค๋ง ํ ์คํธ
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupTest {
// ํ
์คํธ๋ฅผ ์งํํ URL
private final String URL = "https://jwhy-study.tistory.com/38";
@Test
void getHtmlTest() {
try {
Document document = Jsoup.connect(URL).get();
System.out.println(document);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
์ ์ฝ๋์ ๊ฐ์ด Jsoup
์์ ์ ๊ณตํ๋ Document
์ URL
์ ์ฐ๊ฒฐํ ๋ค, ์ถ๋ ฅํ๋ฉด ํด๋น ํ์ด์ง์ ๋ ๋๋ง๋ HTML
์ฝ๋๊ฐ ๋ฌ๋ค.
์ด์ ๋ด๊ฐ ๊ฐ์ ธ์ค๊ณ ์ถ์ ๋ถ๋ถ์ ๊ฐ์ ธ์๋ณด์!
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupTest {
private final String URL = "https://jwhy-study.tistory.com/38";
@Test
void getHtmlTest() {
try {
int cnt = 0;
Document document = Jsoup.connect(URL).get();
// blockquote ํ๊ทธ ๋ด๋ถ์ p ํ๊ทธ์ ์๋ ๋ชจ๋ ์ ๋ณด๋ฅผ ์ ํํด content์ ์ ์ฅํ๋ค.
// Elements content = document.select("blockquote p");
// blockquote ํ๊ทธ์ ์๋ ๋ชจ๋ ๋ฌธ์์ด์ ๊ฐ์ ธ์จ๋ค.
Elements content = document.select("blockquote");
for (Element e : content) {
// ํด๋น ํ๊ทธ์ ์๋ ๋ฌธ์ฅ ์ถ๋ ฅ
String text = e.text();
cnt++;
}
assertTrue(cnt == 10);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
ํ์ธํด๋ณด๋ ์ด 10๊ฐ์ ์ธ์ฉ๋ฌธ์ด ์กด์ฌํ๋๋ฐ, ํ ์คํธ ๊ฒฐ๊ณผ ์ ์์ ์ผ๋ก ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ๋ค!
Element e
์์ ๋ ๊ตฌ์ฒด์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ค๋ฉด ์๋์ ๊ฐ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
// ์ฒซ td ํ๊ทธ ์์ ์๋ ์ฒซ p ํ๊ทธ์ ๋ด์ฉ
element.select("td:eq(0) p:eq(0)").text();
// ์ฒซ td ํ๊ทธ ์์ ์๋ img ํ๊ทธ์ src ์์ฑ ๋ด์ฉ
element.select("td:eq(0) img").attr("src")
์ ์ฝ๋์ ๊ฐ์ด ๋ค์ํ CSS ์ ํ์ ์ฟผ๋ฆฌ๋ฅผ ์ง์ํ๋ ์ ํ์ธํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค!
๐ค ํ๊ณ
ํฌ๋กค๋ง์ ๋ํ ๋ด์ฉ์ LISTLY์ ์ ์ ๋ฆฌ๋์ด์๋ค.
์์ฝํด๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
- ์๋ฃ์ ์ถ์ฒ, DB, ์ ์๊ถ, ๊ฐ์ธ ์ ์์ ๊ดํ ์๋ฃ ์ ๊ทผ ๋ฑ์ ๋ํด์ ์ ์คํ๊ฒ ์ ์ดํด๋ณธ ๋ค ์ฌ์ฉํ์.
Robots.txs
ํ์ผ์ ํตํด ํฌ๋กค๋งํ ์ ์๋ ๋ฒ์๋ฅผ ํ์ธํ๊ณ , ๊ทธ ๋ฒ์ ๋ด์์๋ง ์งํํ์.- ๋๋ฌด ๋ง์ ์์ฒญ์ ๋ณด๋ด ํด๋น ์๋ฒ์ ๋ถํ๋ฅผ ์ฃผ์ง ๋ง์.
ํฌ๋กค๋ง ์์ฒด๊ฐ ๋ถ๋ฒ์ ์๋์ง๋ง, ์ฃผ์ํด์ ์ฌ์ฉํด์ผํ ๊ฒ ๊ฐ๋ค!
'Spring > ๊ธฐ๋ฅ ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JUnit5] - @Nested ํ ์คํธ ์์ ์ง์ (2) | 2023.10.08 |
---|---|
[Spring] - SpringBoot 3.x ๋ฒ์ ์์ Swagger ์ฌ์ฉํ๊ธฐ (1) | 2023.09.10 |
[Spring] - Naver Api๋ก ์ํ ๊ฒ์ ๋ชฉ๋ก ๋ฐ์์ค๊ธฐ (0) | 2023.09.10 |
[Spring] - record DTO (0) | 2023.09.10 |
[Spring] - Spring Scheduler (0) | 2023.09.10 |