코딩(Coding)

[JAVA/Jsoup] 로또 홈페이지 파싱의 모든 것!

Ghomdori 2019. 11. 19. 16:23

HTML을 파싱하기 위해서는 

Jsoup 라이브러리가 필요합니다!

프로젝트 툴은 Eclipse를 사용하였습니다.

jsoup-1.12.1.jar
0.38MB

 

[자바 Project 우클릭] -> [Build Path] -> [Configure Build Path]

[Java Build Path] -> [Libraries] -> [Add External JARs] -> jsoup-1.12.1.jar 파일 선택

[Apply and Close]

 

이제 파싱을 할 준비가 다 끝났습니다!!

QR코드로 생성된 URL로 들어가서 HTML 소스를 보겠습니다.

https://m.dhlottery.co.kr/qr.do?method=winQr&v=0809q021825303444q050812313445q060817202240q121623253641q0408092329440000002233

 

로또 파싱을 위한 데이터를 쭉 봅니다.

로또 당첨 회차

<span class="key_clr1">제809회</span>

로또 추첨일

<span class="date">2018-06-02 추첨</span>

로또 당첨번호 1

<div class="clr clr1">

<span>6</span>

</div

로또 당첨번호 2

<div class="clr clr2">
	<span>11</span>
</div>

로또 당첨번호 3

<div class="clr clr2">
	<span>15</span>
</div>

로또 당첨번호 4

<div class="clr clr2">
	<span>17</span>
</div>

로또 당첨번호 5

<div class="clr clr3">
	<span>23</span>
</div>

로또 당첨번호 6

<div class="clr clr4">
	<span>40</span>
</div>

보너스 번호

<div class="plus clr clr4">
	<span>
    "39"
    ::after
    </span>
</div>

div class에 나타난 것은 색상에 따른 class name입니다.

그래서 div class name으로 파싱하지 말고

<div class="list">

에 대하여 파싱하도록 하겠습니다.

해당 소스는 QR코드로 나온 URL에 대해서는 모두 다 파싱할 수 있습니다.

PC버전 동행복권 main 페이지, 회차별 로또 정보 파싱에 대한 내용은 다음에 올리도록 하겠습니다.

올리기 너무 귀찮네요...(특히 캡쳐)

 

JAVA Jsoup을 이용한 로또 파싱 전체 소스

public class LottoParsing{
	public static void main(String[] args) {
		try {
			Document doc = Jsoup.connect("https://m.dhlottery.co.kr/qr.do?method=winQr&v=0809q021825303444q050812313445q060817202240q121623253641q0408092329440000002233").get();
			String contents = doc.select("div.winner_number h3 span").text();
			String drwNo = contents.substring(1,4)+"회";
			String Year = contents.substring(6,10)+"-";
			String Month = contents.substring(11,13)+"-";
			String Day = contents.substring(14,16);
			String drwDate = Year+Month+Day;
			System.out.println(drwNo);	// 회차
			System.out.println(drwDate);	//추첨일
			String content= doc.select("div.list span").text();
            int drwtNo1 = Integer.parseInt(content.split("\\s")[0]);
            int drwtNo2 = Integer.parseInt(content.split("\\s")[1]);
            int drwtNo3 = Integer.parseInt(content.split("\\s")[2]);
            int drwtNo4 = Integer.parseInt(content.split("\\s")[3]);
            int drwtNo5 = Integer.parseInt(content.split("\\s")[4]);
            int drwtNo6 = Integer.parseInt(content.split("\\s")[5]);
            int bnusNo = Integer.parseInt(content.split("\\s")[6]);
            System.out.println(drwtNo1+","+drwtNo2+","+drwtNo3+","+drwtNo4+","+drwtNo5+","+drwtNo6+","+bnusNo);	//당첨번호 + 보너스
            
		}catch (IOException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
		}
	}
}

파싱이 아주 잘 되는 것을 볼 수 있습니다!

※ URL은 Google에 있는 로또용지에 있는 QR코드 스캔하여, 따온 URL입니다. 아무거나 하셔도 무관합니다.

다만, 꼭 QR코드 스캔을 한 URL이여야 합니다.( Mobile과 PC의 HTML소스가 다릅니다.)

나머지, 기본적인 동행복권 홈페이지 로또번호 파싱은 나중에 올리도록 하겠습니다.

공감 버튼 눌러주시면 저에게 정말 큰 힘이 됩니다.