프로젝트 요구 사항으로 인해 차량 브랜드 정보 및 차량 시스템 정보가 필요합니다. 어제, 나는 하루를 JSOUP를 크롤링하는 웹 사이트 정보를 공부하는 것을 보냈습니다. 이 프로젝트는 Maven+Spring+SpringMVC+MyBatis를 사용하여 작성되었습니다.
JSOUP 개발 안내서 주소
이것은 https://car.autohome.com.cn/zhaoche/pinpai/ 웹 사이트의 주소입니다.
1. 먼저 pom.xml에 종속성을 추가합니다
이미지를 로컬로 저장해야하므로 Commons-Net 패키지를 추가했습니다.
<!-https://mvnrepository.com/artifact/org.jsoup/jsoup-> <pectionement> <groupid> org.jsoup </groupid> <artifactid> jsoup <soup </artifactid> <bersion> 1.10.3 </version> </dependency> <!-https://mvnreation.com/artfact/commons-net -> <pectionency> <groupId> Commons-Net </groupId> <artifactid> Commons-Net </artifactid> <버전> 3.3 </version> </fectionency>
2. 크롤러 코드 구현
@controller@requestmapping ( "/car/") 공개 클래스 카 콘트롤러 {// 이미지 저장 경로 비공개 정적 최종 문자열 saveimgpath = "c : // imgs"; /** * @title : 브랜드 이름과 이미지 삽입 및 이미지 삽입 * @description : * @param @throws ioexception * @return void * @throws * @throws * @date 4:42:57 pm */@requestmapping ( "Add") public void insert () ioexception {// Where you define to that the that the that the that the that the that the that the when "https://car.autohome.com.cn/zhaoche/pinpai/"; // 웹 페이지 텍스트 문서를 가져옵니다. doc = jsoup.connect (url) .get (); // 클래스 이름 요소를 기반으로 텍스트 내용을 가져옵니다. elementsByClass = doc.getElementsByClass ( "uibox-con"); // 클래스 모음 (요소 요소 : elementsByClass) {// 클래스의 자식 태그 수를 얻습니다 int childNodesize_1 = element.childNodesize (); // (int i = 0; i <childnodesize_1; i ++) {// 자동차 로고 이미지 주소를 가져옵니다. // 브랜드 이름 문자열 pinpai = element.child (i) .child (0) .child (1) .text (); // 컨텐츠를 출력하여 올바른 System.out.println ( "자동차 로고 이미지 address----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- URL url1 = new URL (tupian_1) FileoutputStream (saveimgpath, imageName) byte [] int p = 0; 폭스 바겐* 모두 합작 투자 공장과 그 자회사의 이름을 얻기 위해 순환해야합니다. 요소 (i) .child (1) .childnodesize (); if (childnodesize_3 == 1) {// (int j = 0; j <childnodesize_2; j ++) {String chexi = element.child (i) .child (1) .child (j) .child (0) .child (0) .text.out.println ( "자동차 시스템 --------------"; 벤처 공장은 각각 (int j = 0; j <childnodesize_3; 합작 공장 정보 문자열 Hezipinpai = child (i) .child (1) .child (0) .text.out.println; // int l = 0; l <hildnodesize_4; l ++) {string chexi = child (1) .child (l) .child (0) .text (); } system.out.println ( "*******************");3. 운영 결과
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.