JAVA爬虫+jsoup+Kibana+Elasticsearch+vue爬取JD搜索页面

1.环境的搭建

2.相关业务层的代码

3.Vue页面的编写

4.测试

1.环境的搭建

1.1 创建一个springboot项目并添加相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dd</groupId>
    <artifactId>qy151-JD</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>qy151-JD</name>
    <description>qy151-JD</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.2 打开elasticsearch和Kibana黑窗口运行脚本文件

在这里我用的elasticsearch和Kibana版本号是
elasticsearch-7.6.1-windows-x86
kibana-7.6.1-windows-x86_64
在这里插入图片描述

1.3 打开Kibana图形化界面创建索引

在这里插入图片描述

2. 相关业务层的代码

2.1 创建utils工具包使用Jsoup对网页进行解析

这里爬取的是JD搜索界面的图片、价格、名字

public class HtmlParseUtil {
   
    //从京东爬虫----有可能从数据库中。
    public static List<Product> parseJd(String keyword) throws Exception {
   
        String path="https://search.jd.com/Search?keyword="+keyword;
        //Document整个网页对象
        Document document = Jsoup.parse(new URL(path), 30000);
        Element j_goodsList = document.getElementById("J_goodsList");
        Elements li = j_goodsList.getElementsByTag("li");
        List<Product> list=new ArrayList<>();
        for (Element element:li){
   
            String price = element.getElementsByClass("p-price").eq(0).text();
            String pname = element.getElementsByClass("p-name").eq(0).text();
            String img = element.getElementsByTag("img").eq(0).attr("data-lazy-img");
            list.add(new Product(pname,price,img));
        }
        return list;
    }
}

然后在工具包下创建CommonResult工具类返回给前端

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult {
   
    private Integer code;
    private String msg;
    private Object result;
}

2.2 Controller层

@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {
   
    @Autowired
    private ProductImpl productService;
//用于添加爬取JD页面的搜索数据
    @GetMapping("/export/{keyword}")
    public CommonResult export(@PathVariable String keyword)throws Exception{
   
        return productService.export(keyword);
    }
//    用于在Vue界面使用Kibana进行高亮搜索和分页
    @PostMapping("/search/{keyword}/{currentPage}/{pageSize}")
    public CommonResult search(@PathVariable String keyword,@PathVariable Integer currentPage,@PathVariable Integer pageSize) throws Exception{
   
        return productService.search(keyword,currentPage,pageSize);
    }
}

2.3 entity类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product {
   
    private String  title;
    private String  price;
    private String  imgUrl;
}

2.4 service层

接口

public interface ProductServe {
   
    CommonResult export(String keyword) throws Exception;
    CommonResult search(String keyword,Integer currentPage,Integer pageSize) throws Exception;
}

Impl实现类

package com.dd.service.impl;

import com.alibaba.fastjson.JSON;
import com.dd.entity.Product;
import com.dd.service.ProductServe;
import com.dd.utils.CommonResult;
import com.dd.utils.HtmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿Ada

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值