https://www.op.gg/champion/statistics
챔피언 분석 - League of Legends
롤 전적, 모든 게임의 전적, 챔프 평점, KDA, 승률을 볼 수 있고 리플을 보거나 자신의 게임을 녹화를 할 수 있습니다. 지금 바로 당신의 소환사명을 검색해보세요!
www.op.gg
(롤 해본 적도 없고 1도 모르지만 미니프로젝트 차 해본)
롤 [미드] 챔피언 순위 크롤링하여 DB에 저장하기
챔피언, 승률, 픽률, 티어 크롤링
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import model.JDBC;
public class LOL {
public static void main(String[] args) {
Connection conn = JDBC.getConnection();
PreparedStatement pstmt = null;
String url = "https://www.op.gg/champion/statistics";
Document doc = null;
Elements img = null;
try {
doc = Jsoup.connect(url).get();
img = doc.getElementsByTag("img"); //img 태그 요소만 가져옴
} catch (IOException e) {
e.printStackTrace();
}
Iterator<Element> itr = doc.select("tbody.champion-trend-tier-MID tr").iterator();
while (itr.hasNext()) {
Element tr = itr.next();
String name = tr.select(".champion-index-table__name").toString(); // 챔피언 이름 받아오기
name = name.substring(0,name.lastIndexOf("<"));
name = name.substring(name.lastIndexOf(">")+1).trim(); // trim() : 공백제거
ArrayList<Element> list = tr.select(".champion-index-table__cell--value"); // 승률,픽률,티어 한번에 받아서 리스트로 저장
String win_rate = list.get(0).toString(); // 승률
win_rate = win_rate.substring(0,win_rate.lastIndexOf("<"));
win_rate = win_rate.substring(win_rate.lastIndexOf(">")+1).trim();
String pick_rate = list.get(1).toString(); // 픽률
pick_rate = pick_rate.substring(0,pick_rate.lastIndexOf("<"));
pick_rate = pick_rate.substring(pick_rate.lastIndexOf(">")+1).trim();
String tier = list.get(2).toString(); // 티어
// System.out.println(tier);
// System.out.println(tier.indexOf("1")); // index 148
tier = tier.substring(148, 149);
System.out.println(name + " / " + win_rate + " / " + pick_rate + " / " + tier);
// DB에 insert
try {
pstmt = conn.prepareStatement("insert into champion values (?, ?, ?, ?)");
pstmt.setString(2, win_rate);
pstmt.setString(3, pick_rate);
pstmt.setString(4, tier);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
JDBC.Close(conn, pstmt);
}
}
긁어온 데이터 DB에 저장 결과
'개발 공부 > JAVA' 카테고리의 다른 글
[JAVA] DBCP (DataBase Connection Pool) (0) | 2021.09.08 |
---|---|
[JAVA] 어노테이션 (Annotation) (0) | 2021.09.07 |
[JAVA] PreparedStatement를 이용한 트랜잭션(Transaction) 처리 (0) | 2021.07.29 |
[JAVA] JDBC / VO / DAO (0) | 2021.07.29 |
[JAVA] 컬렉션 프레임 워크 (1) (ArrayList / Vector / LinkedList) (0) | 2021.07.24 |