백엔드에서 크롤링하기(1)

차곡차곡·2024년 4월 12일
0

SpringBoot + React

목록 보기
2/9

프로젝트 진행 중에 크롤링이란 기술을 사용해보기로 했다.

[ Fashion.java ]

@Getter
@Builder
@ToString
public class Fashion {
    private String url;
    private String image;
    private String subject;

}

[ FashionController.java ]

@Controller
public class FashionController {

    @Autowired
    private FashionService fashionService;

    @GetMapping("/fashion")
    public String fashion(Model model) throws Exception {
        List<Fashion> fashionList = fashionService.getFashionDatas();
        model.addAttribute("fashion", fashionList);

        return "fashion";
    }
}

[ FashionService.java ]


@Service
public class FashionService {
    private static String Fashion_URL = "[크롤링할 페이지 주소";

    @PostConstruct
    public List<Fashion> getFashionDatas() throws IOException {
        List<Fashion> fashionList = new ArrayList<>();
        Document document = Jsoup.connect(Fashion_URL).get();
        Elements contents = document.select("ul.style-list li.style-list-item");
        
        for (Element content : contents) {
            Fashion fashion = Fashion.builder()
                    .image(content.select("a img").attr("abs:src")) // 해당 페이지에서 가져올 css 부분을 입력해주면 된다.
                     .subject(content.select("h4 a").text()) 
                    .subject(content.select("a span ").text())

                    .url(content.select("a").attr("abs:href")) // 링크
                    .build();
            fashionList.add(fashion);
        }

        return fashionList;
    }
}

[ fashion.html ] - 임시로 만든 html

<title>re:Pick</title>

<div layout:fragment="content">
  <div>
    <table class= "table">
      <tr>
        <th>이미지</th>
        <th>제목</th>
      </tr>
      <tr th:each="fashion : ${fashion} ">
        <td><a th:href="${fashion.url}"><img th:src="${fashion.image}"></a></td>
        <td><a th:href="${fashion.url}"><span th:text="${fashion.subject}"></span></a></td>
        </a>
      </tr>
    </table>
  </div>
</div>
</html>


서버를 실행시켜보면 이런 식으로 원하는 데이터들만 가져올 수 있다.

아무래도 링크는 필요 없을 것 같다.

profile
계단식 성장

0개의 댓글