jsoup如何爬取圖片到本地
因為項目需求,需要車輛品牌信息和車系信息,昨天用一天時間研究了jsoup爬取網(wǎng)站信息。項目是用maven+spring+springmvc+mybatis寫的。
這個是需要爬取網(wǎng)站的地址 https://car.autohome.com.cn/zhaoche/pinpai/
1.首先在pom.xml中添加依賴
因為需要把圖片保存到本地所以又添加了commons-net包
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
2.爬蟲代碼的實現(xiàn)
@Controller
@RequestMapping("/car/")
public class CarController {
//圖片保存路徑
private static final String saveImgPath="C://imgs";
/**
* @Title: insert 品牌名稱 和圖片爬取和添加
* @Description:
* @param @throws IOException
* @return void
* @throws
* @date 2018年1月29日 下午4:42:57
*/
@RequestMapping("add")
public void insert() throws IOException {
//定義想要爬取數(shù)據(jù)的地址
String url = "https://car.autohome.com.cn/zhaoche/pinpai/";
//獲取網(wǎng)頁文本
Document doc = Jsoup.connect(url).get();
//根據(jù)類名獲取文本內(nèi)容
Elements elementsByClass = doc.getElementsByClass("uibox-con");
//遍歷類的集合
for (Element element : elementsByClass) {
//獲取類的子標簽數(shù)量
int childNodeSize_1 = element.childNodeSize();
//循環(huán)獲取子標簽內(nèi)的內(nèi)容
for (int i = 0; i < childNodeSize_1; i++) {
//獲取車標圖片地址
String tupian = element.child(i).child(0).child(0).child(0).child(0).attr("src");
//獲取品牌名稱
String pinpai = element.child(i).child(0).child(1).text();
//輸出獲取內(nèi)容看是否正確
System.out.println("車標圖片地址-----------" + tupian);
System.out.println("品牌-----------" + pinpai);
System.out.println();
//把車標圖片保存到本地
String tupian_1 = "http:"+tupian;
//連接url
URL url1 = new URL(tupian_1);
URLConnection uri=url1.openConnection();
//獲取數(shù)據(jù)流
InputStream is=uri.getInputStream();
//獲取后綴名
String imageName = tupian.substring(tupian.lastIndexOf("/") + 1,tupian.length());
//寫入數(shù)據(jù)流
OutputStream os = new FileOutputStream(new File(saveImgPath, imageName));
byte[] buf = new byte[1024];
int p=0;
while((p=is.read(buf))!=-1){
os.write(buf, 0, p);
}
/**
* 因為每個品牌下有多個合資工廠
* 比如一汽大眾和上海大眾還有進口大眾
* 所有需要循環(huán)獲取合資工廠名稱和旗下
* 車系
*/
//獲取車系數(shù)量
int childNodeSize_2 = element.child(i).child(1).child(0).childNodeSize();
/**
* 獲取標簽下子標簽數(shù)量
* 如果等于1則沒有其他合資工廠
*/
int childNodeSize_3 = element.child(i).child(1).childNodeSize();
if(childNodeSize_3==1){
//循環(huán)獲取車系信息
for (int j = 0; j < childNodeSize_2; j++) {
String chexi = element.child(i).child(1).child(0).child(j).child(0).child(0).text();
System.out.println("車系-----------" + chexi);
}
}else{
/**
* 如果childNodeSize_3大于1
* 則有多個合資工廠
*/
//分別獲取各個合資工廠旗下車系
for (int j = 0; j < childNodeSize_3; j++) {
int childNodeSize_4 = element.child(i).child(1).child(j).childNodeSize();
/**
* 如果j是單數(shù)則是合資工廠名稱
* 否則是車系信息
*/
int k = j%2;
if(k==0){
//獲取合資工廠信息
String hezipinpai = element.child(i).child(1).child(j).child(0).text();
System.out.println("合資企業(yè)名稱-----------" + hezipinpai);
}else{
//int childNodeSize_5 = element.child(i).child(1).child(0).childNodeSize();
//循環(huán)獲取合資工廠車系信息
for(int l = 0; l < childNodeSize_4; l++){
String chexi = element.child(i).child(1).child(j).child(l).child(0).child(0).text();
System.out.println("車系-----------" + chexi);
}
}
}
}
System.out.println("************************");
System.out.println("************************");
}
}
}
}
3.運行結(jié)果



以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的@PropertySource注解源碼詳細解析
這篇文章主要介紹了Spring中的@PropertySource注解源碼詳細解析,@PropertySource注解,標注在配置類@Configuration上面,下面主要分析一下@PropertySource注解的處理過程,也就是怎么把配置信息從.properies文件放到environment中的,需要的朋友可以參考下2024-01-01
IDEA使用MyBatisCodeHelperPro來generator代碼的詳細教程
這篇文章主要介紹了IDEA使用MyBatisCodeHelperPro來generator代碼的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
使用Java自定義注解實現(xiàn)一個簡單的令牌桶限流器
限流是在分布式系統(tǒng)中常用的一種策略,它可以有效地控制系統(tǒng)的訪問流量,保證系統(tǒng)的穩(wěn)定性和可靠性,在本文中,我將介紹如何使用Java自定義注解來實現(xiàn)一個簡單的令牌桶限流器,需要的朋友可以參考下2023-10-10
mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle
這篇文章主要介紹了mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

