因为项目需求,需要车辆品牌信息和车系信息,昨天用一天时间研究了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 -->
<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.爬虫代码的实现
@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 {
//定义想要爬取数据的地址
String url = "https://car.autohome.com.cn/zhaoche/pinpai/";
//获取网页文本
Document doc = Jsoup.connect(url).get();
//根据类名获取文本内容
Elements elementsByClass = doc.getElementsByClass("uibox-con");
//遍历类的集合
for (Element element : elementsByClass) {
//获取类的子标签数量
int childNodeSize_1 = element.childNodeSize();
//循环获取子标签内的内容
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();
//输出获取内容看是否正确
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();
//获取数据流
InputStream is=uri.getInputStream();
//获取后缀名
String imageName = tupian.substring(tupian.lastIndexOf("/") + 1,tupian.length());
//写入数据流
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);
}
/**
* 因为每个品牌下有多个合资工厂
* 比如一汽大众和上海大众还有进口大众
* 所有需要循环获取合资工厂名称和旗下
* 车系
*/
//获取车系数量
int childNodeSize_2 = element.child(i).child(1).child(0).childNodeSize();
/**
* 获取标签下子标签数量
* 如果等于1则没有其他合资工厂
*/
int childNodeSize_3 = element.child(i).child(1).childNodeSize();
if(childNodeSize_3==1){
//循环获取车系信息
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是单数则是合资工厂名称
* 否则是车系信息
*/
int k = j%2;
if(k==0){
//获取合资工厂信息
String hezipinpai = element.child(i).child(1).child(j).child(0).text();
System.out.println("合资企业名称-----------" + hezipinpai);
}else{
//int childNodeSize_5 = element.child(i).child(1).child(0).childNodeSize();
//循环获取合资工厂车系信息
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.运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。