Posts DEVIL: 프로그래머스 채용공고 크롤링
Post
Cancel

DEVIL: 프로그래머스 채용공고 크롤링

변경사항

  • 사람인의 채용공고의 경우 채용공고 게시글의 내용이 이미지 파일인 경우가 많아, 텍스트로 가져오기가 힘들었다.
  • 채용공고 내용이 텍스트로 되어 있고, 채용공고가 개발자 직군에 한정된 프로그래머스 사이트의 채용공고를 크롤링하기로 결정하였다.
  • 채용공고 내용을 가져오기 위해 크롤링 사이트를 바꾼 만큼, 목록 페이지가 아니라 상세보기 페이지에 반복문을 돌며 접근해야 한다.

기능추가

1.사람인과 달리 한 페이지에서 원하는 만큼의 채용공고를 볼 수 없다. 따라서 우선 전체 목록을 순회하기 위해 마지막 페이지 번호(쪽수 번호)(사진에서 32)를 알아냈다.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/64f3c01e-fca0-4ad8-b6d8-0bab479ccf91/Untitled.png

1
2
3
  Elements pages = listDoc.select("ul.pagination li.page-item a");
  int lastPage = Integer.parseInt(pages.get(7).attr("href").split("=")[1]);
  System.out.println(lastPage); //3

2.lastPage 까지 반복문을 돌면서 각 목록 페이지에 접근해 URL을 가져왔다. 가져온 URL은 ArrayList에 추가하였다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      // 크롤링할 목록 한 페이지의 URL 알아내기
  for (int i = 1; i <= lastPage; i++) {
    try {
      doc = Jsoup.connect(url + i).get();
    } catch (IOException e) {
      e.printStackTrace();
    }
    Elements titles = doc.select("h5.position-title a");
    for (Element element : titles) {
      //System.out.println(element.attr("href"));
      urls.add("<https://programmers.co.kr>" + element.attr("href"));
    } 
  }
 

ArrayList 에 담겨 있는 주소에 반복문을 사용하여 접근하여 정보를 가져온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    for (String positionUrl : urls) {
    //System.out.println(positionUrl);

    Document positionDoc = null;
    try {
      positionDoc = Jsoup.connect(positionUrl).get();
    } catch (IOException e) {
      e.printStackTrace();
    }
    Elements elements = positionDoc.select("table.table-information tbody tr ");
    System.out.println("====================");
    System.out.printf("제목: %s\\n", positionDoc.select("h2.title").text());
    System.out.printf("회사명: %s\\n", positionDoc.select("h4.sub-title").text());
    for (Element ele : elements) {
      String raw = ele.text();
      if (raw.startsWith("기간 ")) {
        //System.out.println(raw);
        String parsed = raw.replace("기간 ", "");
        if (raw.contains("까지") && raw.contains("부터")) {
          parsed = raw.split(" ")[4];
        }
        System.out.printf("기간: %s\\n", parsed);
        break;
      }
    }
    System.out.println("내용: ");
    Element element = positionDoc.select("div.page-show-detail").select("div.content-body").get(0);
    Elements sections = new Elements();
    sections.addAll(element.select("section.section-position"));
    sections.addAll(element.select("section.section-requirements"));
    sections.addAll(element.select("section.section-preference"));
    for (Element section : sections) {
      String title = section.select("h5.section-title").text();
      System.out.println("[" + section.select("h5.section-title").text()+ "]");
      Elements lis = section.select("li");
      for (Element li : lis) {
        System.out.println("   " + li.text());;
      }
      if (lis.size() == 0) {
        for (Element p : section.select("p")) {
          System.out.println(p.text());            
        }
      }
      
    }
    System.out.println("====================");
  }

}
This post is licensed under CC BY 4.0 by the author.

DEVIL: 사람인 채용공고 크롤링

HTML5 CSS3 웹 표준의 정석 #6장: 텍스트 관련 스타일

Loading comments from Disqus ...