这几天有个电商项目,缺少素材,网上死活找不到,去看了一下午爬虫,没看懂就只能爬页面,爬不到需要的数据,去找了了些资料,发现java也有类似的功能依赖包Jsoup爬虫,我全是写在test文件夹下测试的
1导入依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
建立需要的数据库和表,
建立实体类
用mybatis-plus操作
2,爬取数据到数据库 (mybatis-plus逆向工程操作数据库)
package com.example.jd_demo;
import com.example.jd_demo.entity.Goodslist;
import com.example.jd_demo.mapper.GoodslistMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class HtmlParseUtil {
@Autowired
private GoodslistMapper goodslistMapper;
@Test
void contextLoads() throws Exception {
//进行测试,调用方法,输入想要爬取数据的关键字
// 循环爬数据爬10页就够了
for (int i = 1; i <= 10; i++) {
HtmlParseUtil util = new HtmlParseUtil();
List<Goodslist> list = util.parseJD("相机", i);
for (Goodslist goodslist : list) {
int insert = goodslistMapper.insert(goodslist);
if (insert > 0) {
System.out.println(goodslist);
}
}
}
}
// public static void main(String[] args) throws Exception {
//
//
// }
//循环页数爬数据
public List<Goodslist> parseJD(String keywords, int page) throws Exception {
//获取请求,我们使用京东页面作为演示例子:http://search.jd.com/Search?keyword=[keywords]
//注意:爬取数据过程需要联网
String url = "http://search.jd.com/Search?keyword=" + keywords + "&page=" + page;
//得到url后,解析网页,该 Document对象 就是浏览器 Document对象
Document document = Jsoup.parse(new URL(url), 30000);
Element element = document.getElementById("J_goodsList");
//获取所有的li元素
Elements elements = element.getElementsByTag("li");
//创建ArrayList集合,将所有的查询对象添加到集合中
ArrayList<Goodslist> goodsList = new ArrayList<>();
//获取li元素中的内容,这里的el就是每一个li元素
for (Element el : elements) {
//关于这种图片特别多的网站,所有的图片都是延时加载的
//代码中的p-price,p-name等属性名是通过手动审查网页元素得到
String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
String price = el.getElementsByClass("p-price").eq(0).text();
转类型有个人民币符号没办法,除非截取掉
// BigDecimal bigPrice = BigDecimal.valueOf(Long.parseLong(price));
String title = el.getElementsByClass("p-name").eq(0).text();
// String picShop = el.getElementsByClass("p-shop").eq(0).text();
String picSource = el.getElementsByClass("p-stock").attr("data-province");
// 存入数据库
Goodslist goodslists = new Goodslist();
// 拼接协议头http://
goodslists.setPicUrl(img);
goodslists.setPicPrice(price);
goodslists.setPicName(title);
goodslists.setPicShop("新源负笈店");
goodslists.setPicSource(picSource);
// 这里没有注入成功数据层mapper,添加不成功,可以用list先存在转存
// 这里面弄不了数据库????、进了一条数据但在持续访问数据,添加不成功
// int insert = goodslistMapper.insert(goodslists);浪费2个小时
// if (insert < 0) {
没有增加成功就false
// return false;
// }
goodsList.add(goodslists);
}
// 成功就返回true
return goodsList;
}
}
3下载图片
package com.example.jd_demo;
import com.example.jd_demo.entity.Goodslist;
import com.example.jd_demo.mapper.GoodslistMapper;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
@SpringBootTest
class DemophotoApplicationTests {
@Autowired
private GoodslistMapper goodslistMapper;
@Test
void contextLoads() {
// 下载电商图片
// 1.获取图片路径
List<Goodslist> goodslists = goodslistMapper.selectList(null);
for (int i = 0; i < goodslists.size(); i++) {
String picUrl ="http:"+goodslists.get(i).getPicUrl();
System.out.println(picUrl);
// 京东路径少了http://,可以添加数据库时增加,也可以这里增加
downLoadByUrl(picUrl, "E:\\资源\\京东商城图片\\数码\\耳机\\无限");
}
}
/*
从网络Url中下载文件
*/
public static void downLoadByUrl(String urlStr, String savePath) {
try {
String fileName = getFileName(urlStr);
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为5秒
conn.setConnectTimeout(5 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) { // 没有就创建该文件
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
fos.close();
inputStream.close();
System.out.println("the file: " + url + " download success");
} catch (Exception e) {
e.printStackTrace();
}
}
/*
从输入流中获取字节数组
*/
private static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[4 * 1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
//* 从src文件路径获取文件名,网端
private static String getFileName(String srcRealPath) {
return StringUtils.substringAfterLast(srcRealPath, "/");
}
private static String getFileName1(String srcRealPath) {
// UUID uuid = UUID.randomUUID();
// 截取字符串.后面的文件格式
// System.out.println((str.substring(str.indexOf("智“)+1,str.indexOf(“发”)));//从“智”开始截取到”发“
int i1 = srcRealPath.lastIndexOf("/");
int i2 = srcRealPath.lastIndexOf("?");
String substring = srcRealPath.substring(i1, i2);
// String.valueOf(uuid)+".png"
return substring;
}
}
效果