Showing
2 changed files
with
67 additions
and
18 deletions
WebCrawling/chromedriver
0 → 100755
This file is too large to display.
| ... | @@ -8,6 +8,13 @@ import java.io.*; | ... | @@ -8,6 +8,13 @@ import java.io.*; |
| 8 | import java.net.URI; | 8 | import java.net.URI; |
| 9 | import java.net.URISyntaxException; | 9 | import java.net.URISyntaxException; |
| 10 | import java.util.*; | 10 | import java.util.*; |
| 11 | +import java.util.List; | ||
| 12 | + | ||
| 13 | +import org.openqa.selenium.By; | ||
| 14 | +import org.openqa.selenium.WebDriver; | ||
| 15 | +import org.openqa.selenium.WebElement; | ||
| 16 | +import org.openqa.selenium.chrome.ChromeDriver; | ||
| 17 | +import org.openqa.selenium.chrome.ChromeOptions; | ||
| 11 | 18 | ||
| 12 | class CGVMovieInfo { //CGV 영화 정보를 담는 class | 19 | class CGVMovieInfo { //CGV 영화 정보를 담는 class |
| 13 | private String title; //영화 제목 | 20 | private String title; //영화 제목 |
| ... | @@ -80,20 +87,58 @@ class CGVMovieInfo { //CGV 영화 정보를 담는 class | ... | @@ -80,20 +87,58 @@ class CGVMovieInfo { //CGV 영화 정보를 담는 class |
| 80 | } | 87 | } |
| 81 | 88 | ||
| 82 | public class CGVExample { | 89 | public class CGVExample { |
| 90 | + | ||
| 91 | + public static final String WEB_DRIVER_ID = "webdriver.chrome.driver"; //드라이버 ID | ||
| 92 | + public static final String WEB_DRIVER_PATH = "WebCrawling/chromedriver"; //드라이버 경로 | ||
| 93 | + | ||
| 83 | public static void main(String[] args) { | 94 | public static void main(String[] args) { |
| 95 | + | ||
| 84 | Scanner scanner = new Scanner(System.in); | 96 | Scanner scanner = new Scanner(System.in); |
| 85 | String url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것. 예매율 순위 가져오기 | 97 | String url_movies = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것. 예매율 순위 가져오기 |
| 86 | - String url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오기. | 98 | + String url_theaters = "https://www.cgv.co.kr/theaters"; //영화관 정보 가져오는 링크. |
| 87 | - | ||
| 88 | - Document doc_movies, doc_theaters; | ||
| 89 | 99 | ||
| 90 | - ArrayList<LinkedHashMap<String, Integer>> Theaters = new ArrayList<>(); //지역별 영화관 HashMap(Key: 영화관, value:영화관별 고유코드)으로 이루어진 Arraylist | 100 | + ArrayList<LinkedHashMap<String, String>> theaters = new ArrayList<>(); //지역별 영화관 HashMap(Key: 영화관, value:영화관별 고유코드)으로 이루어진 Arraylist |
| 91 | ArrayList<CGVMovieInfo> Movies = new ArrayList<>(); //CGVMovieInfo 클래스의 인스턴스들을 원소로 가지는 Arraylist | 101 | ArrayList<CGVMovieInfo> Movies = new ArrayList<>(); //CGVMovieInfo 클래스의 인스턴스들을 원소로 가지는 Arraylist |
| 92 | 102 | ||
| 103 | + try{ //드라이버 설정 | ||
| 104 | + System.setProperty(WEB_DRIVER_ID,WEB_DRIVER_PATH); | ||
| 105 | + }catch (Exception e){ | ||
| 106 | + e.printStackTrace(); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + ChromeOptions options = new ChromeOptions(); //크롬 설정을 담은 객체 생성 | ||
| 110 | + options.addArguments("headless"); //브라우저가 눈에 보이지 않고 컴파일러 내부에서 작동됨. | ||
| 111 | + | ||
| 112 | + WebDriver driver = new ChromeDriver(options); //위에서 설정한 옵션을 파라미터로 넘겨주고, 드라이버 객체 생성. | ||
| 113 | + driver.get(url_theaters); //WebDriver 객체를 해당 URL로 이동시킨다. | ||
| 114 | + | ||
| 115 | + //브라우저 이동시 생기는 로드시간을 기다린다. | ||
| 116 | + //HTTP 응답속도 보다 자바의 컴파일 속도가 더 빠르기 때문에 임의적으로 1초를 대기한다. | ||
| 117 | + try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} | ||
| 118 | + | ||
| 119 | + //영화관 및 영화관에 대응되는 영화관별 고유 코드 가져오기. | ||
| 120 | + List<WebElement> area = driver.findElements(By.className("area")); | ||
| 121 | + for (WebElement elem : area) { | ||
| 122 | + LinkedHashMap<String, String> theaters_info = new LinkedHashMap<>(); | ||
| 123 | + List<WebElement> theaters_by_area = elem.findElements(By.tagName("a")); | ||
| 124 | + for (WebElement theater : theaters_by_area) { | ||
| 125 | + String theater_name = theater.getAttribute("title").replace("CGV", ""); | ||
| 126 | + String theater_code = theater.getAttribute("href").replaceAll("(.+(?<=theaterCode=))|(.+(?<=theatercode=))", "").substring(0,4); | ||
| 127 | + theaters_info.put(theater_name, theater_code); | ||
| 128 | + } | ||
| 129 | + theaters.add(theaters_info); | ||
| 130 | + } | ||
| 131 | + | ||
| 93 | try { | 132 | try { |
| 94 | - doc_movies = Jsoup.connect(url_movies).get(); | 133 | + driver.close(); //드라이버 연결 해제 |
| 95 | - doc_theaters = Jsoup.connect(url_theaters).get(); | 134 | + driver.quit(); //프로세스 종료 |
| 135 | + } catch (Exception e) { | ||
| 136 | + throw new RuntimeException(e.getMessage()); | ||
| 137 | + } | ||
| 96 | 138 | ||
| 139 | + Document doc_movies; | ||
| 140 | + try { | ||
| 141 | + doc_movies = Jsoup.connect(url_movies).get(); | ||
| 97 | //예매율 Top19까지의 영화의 정보를 가져옴. | 142 | //예매율 Top19까지의 영화의 정보를 가져옴. |
| 98 | Elements elements1 = doc_movies.select("div.sect-movie-chart"); | 143 | Elements elements1 = doc_movies.select("div.sect-movie-chart"); |
| 99 | Iterator<Element> rank = elements1.select("strong.rank").iterator(); | 144 | Iterator<Element> rank = elements1.select("strong.rank").iterator(); |
| ... | @@ -102,10 +147,6 @@ public class CGVExample { | ... | @@ -102,10 +147,6 @@ public class CGVExample { |
| 102 | Iterator<Element> GoldenEgg = elements1.select("span.percent").iterator(); | 147 | Iterator<Element> GoldenEgg = elements1.select("span.percent").iterator(); |
| 103 | Iterator<Element> link = elements1.select("a.link-reservation").iterator(); | 148 | Iterator<Element> link = elements1.select("a.link-reservation").iterator(); |
| 104 | 149 | ||
| 105 | - //서울/경기/인천/강원/대전,충청/대구/부산,울산/경상/광주,전라,제주 - 광역 정보 가져오기 | ||
| 106 | - Elements elements2 = doc_theaters.getElementsByAttributeValue("class", "sect-city"); | ||
| 107 | - Iterator<Element> region = elements2.select("li.on").iterator(); | ||
| 108 | - | ||
| 109 | while(title.hasNext()){ | 150 | while(title.hasNext()){ |
| 110 | String newTitle = title.next().text(); | 151 | String newTitle = title.next().text(); |
| 111 | int newRank = Integer.parseInt(rank.next().text().replace("No.","")); | 152 | int newRank = Integer.parseInt(rank.next().text().replace("No.","")); |
| ... | @@ -115,24 +156,32 @@ public class CGVExample { | ... | @@ -115,24 +156,32 @@ public class CGVExample { |
| 115 | CGVMovieInfo newMovie = new CGVMovieInfo(newTitle, newRank, newScore, GoldenEgg.next().text(), newCode); | 156 | CGVMovieInfo newMovie = new CGVMovieInfo(newTitle, newRank, newScore, GoldenEgg.next().text(), newCode); |
| 116 | Movies.add(newMovie); | 157 | Movies.add(newMovie); |
| 117 | } | 158 | } |
| 118 | - | ||
| 119 | - while(region.hasNext()){ | ||
| 120 | - System.out.println(region.next().text()); | ||
| 121 | - } | ||
| 122 | - | ||
| 123 | }catch(IOException e){ | 159 | }catch(IOException e){ |
| 124 | e.printStackTrace(); | 160 | e.printStackTrace(); |
| 125 | } | 161 | } |
| 126 | 162 | ||
| 127 | for (CGVMovieInfo elem : Movies) { | 163 | for (CGVMovieInfo elem : Movies) { |
| 128 | - //elem.printMovieInfo(); | 164 | + elem.printMovieInfo(); |
| 129 | - System.out.println(elem.getRank() + " : " + elem.getTitle()); | 165 | + //System.out.println(elem.getRank() + " : " + elem.getTitle()); |
| 130 | } | 166 | } |
| 131 | 167 | ||
| 168 | + //영화 이름(Integer 선택지), 영화관 지역 코드, 영화관 이름, 관람 일자 입력 시, (시간 선택 가능한) 예매 사이트로 이동. | ||
| 132 | System.out.print("예매하고 싶은 영화의 순위를 입력하세요 : "); | 169 | System.out.print("예매하고 싶은 영화의 순위를 입력하세요 : "); |
| 133 | int inputRank = scanner.nextInt(); | 170 | int inputRank = scanner.nextInt(); |
| 171 | + | ||
| 172 | + System.out.print("지역 코드를 입력하세요 : "); | ||
| 173 | + int regionCode = scanner.nextInt(); | ||
| 174 | + | ||
| 175 | + System.out.print("영화관명을 입력하세요 : "); | ||
| 176 | + String theaterName = scanner.next(); | ||
| 177 | + String theaterCode = theaters.get(regionCode).get(theaterName); | ||
| 178 | + | ||
| 179 | + System.out.print("관람 일자를 입력하세요 : "); | ||
| 180 | + int date = scanner.nextInt(); | ||
| 181 | + | ||
| 134 | try{ | 182 | try{ |
| 135 | - Desktop.getDesktop().browse(new URI(Movies.get(inputRank - 1).getLink())); | 183 | + String otherFormat = String.format("&THEATER_CD=%s&PLAY_YMD=%s", theaterCode, date); |
| 184 | + Desktop.getDesktop().browse(new URI(Movies.get(inputRank - 1).getLink() + otherFormat)); | ||
| 136 | } | 185 | } |
| 137 | catch(IndexOutOfBoundsException | URISyntaxException | IOException e){ | 186 | catch(IndexOutOfBoundsException | URISyntaxException | IOException e){ |
| 138 | System.out.println(e.getClass()); | 187 | System.out.println(e.getClass()); | ... | ... |
-
Please register or login to post a comment