728x90
★ JavaScript 구문을 가져오기
- JSoup으로는 사용이 불가함.
- 셀레니움(Selenium)을 통해 가져오기
★ 셀레니움(Selenium)
- 테스트 자동화 툴
- https://www.selenium.dev/downloads/
Downloads
Selenium automates browsers. That's it!
www.selenium.dev
- 자바 버전 다운로드
- 압축 해제 후, lib 폴더에 있는 jar파일 lib에 넣기
- 다 넣어도 됨.
★ ChromeDriver 다운로드
- 현재 내 버전 확인
- 도움말 -> Chrome 정보
- 현재 버전 : 버전 113.0.5672.127
- https://chromedriver.chromium.org/
ChromeDriver - WebDriver for Chrome
WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server that implements the W3C WebDriver
chromedriver.chromium.org
- win32 버전 다운
- 사용할 파일 위치 경로에 exe 파일을 위치시키기 (c:/class/dev)
■ 테스트 페이지 소스
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<h1 id="title">Java Crawling</h1>
<p class="desc">자바 크롤링 테스트</p>
<p class="desc">Jsoup 라이브러리 사용</p>
<hr>
<h2>다른 데이터</h2>
<div id = "items"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
load();
function load() {
const list = ['자바', '오라클', '스프링'];
list.forEach(item=>{
$('#items').append(`<div class="item">\${item}</div>`);
});
}
</script>
</body>
</html>
■ 크롤링 소스
try {
//셀레니움 기초 셋팅
String webDriverID = "webdriver.chrome.driver";
String path = "C:\\class\\dev\\chromedriver.exe";
System.setProperty(webDriverID, path);
ChromeOptions options = new ChromeOptions();
options.setCapability("ignoreProtectedModeSettings", true);
//실제적인 사용
//브라우저 참조 객체
WebDriver driver = new ChromeDriver(options);
String url = "http://localhost:8090/crawling/ex01.do";
driver.get(url);
WebElement title = driver.findElement(By.id("title"));
System.out.println(title.getText());
List<WebElement> list = driver.findElements(By.className("item"));
for (WebElement item : list) {
System.out.println(item.getText());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- 실행 결과
■ 스크립팅
- 소스코드
try {
//셀레니움 기초 셋팅
String webDriverID = "webdriver.chrome.driver";
String path = "C:\\class\\dev\\chromedriver.exe";
System.setProperty(webDriverID, path);
ChromeOptions options = new ChromeOptions();
options.setCapability("ignoreProtectedModeSettings", true);
//실제적인 사용
//브라우저 참조 객체
WebDriver driver = new ChromeDriver(options);
String url = "http://lms1.sist.co.kr/worknet/SLogin.asp";
driver.get(url);
//아이디, 암호 입력
WebElement id = driver.findElement(By.id("strLoginID"));
id.sendKeys("이름");
WebElement pwd = driver.findElement(By.id("strLoginPwd"));
pwd.sendKeys("비밀번호");
//버튼 클릭
WebElement btn = driver.findElement(By.cssSelector("#content > div > form > table > tbody > tr > td > div > div.login-form > div.login-btn > input"));
btn.click();
//딜레이
try {
//driver.wait(3000);
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
WebElement period = driver.findElement(By.cssSelector("#content > div > div > div > div.panel-body > form > table > thead > tr:nth-child(5) > td:nth-child(2)"));
System.out.println("period:" + period.getText());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
- 실행 결과
- 해당 사이트에 접속 성공
728x90
'크롤링&스크래핑' 카테고리의 다른 글
Crawling STEP 2 - 다음 영화페이지 크롤링 (0) | 2023.05.26 |
---|---|
Crawling STEP 1 - JSOUP 사용 (0) | 2023.05.26 |