爬取京东数据
public class HtmlParseUtil {
public List parseJD(String keywords) throws IOException {
//jsoup不能抓取ajax请求,除非自己模拟浏览器进行请求
String url = "https://search.jd.com/Search?keyword="+keywords;
//解析网页
document document = Jsoup.parse(new URL(url), 30000);
//抓取搜索到的数据
//document就相当于JS的document对象
Element j_goodsList = document.getElementById("J_goodsList");
//找到所有的li属性,一个li就对应一个商品的所有属性
Elements lis = j_goodsList.getElementsByTag("li");
List products = new ArrayList<>();
//获取京东的商品信息
for (Element li : lis) {
// String img = li.getElementsByTag("img").eq(0).attr("src");
//
String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");
String price = li.getElementsByClass("p-price").eq(0).text();
String name = li.getElementsByClass("p-name").eq(0).text();
Product product = new Product();
product.setImg(img);
product.setTitle(name);
product.setPrice(price);
products.add(product);
}
return products;
}
}
elasticsearch注入
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
return restHighLevelClient;
}
}
相应的service和controller
@Service
public class ProductService {
@Autowired
private RestHighLevelClient restHighLevelClient;
private final String INDEXNAME = "jd_goods";
public Boolean parseProduct(String keywords) throws IOException {
//解析查询出来的数据
List products = new HtmlParseUtil().parseJD(keywords);
if(CollectionUtils.isEmpty(products)){
throw new RuntimeException("未查询到"+keywords+"的相关信息");
}
//封装数据到索引库中
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout(Timevalue.timevalueSeconds(20));
for (Product product : products) {
bulkRequest.add(new IndexRequest(INDEXNAME).source(JSON.toJSONString(product), XContentType.JSON));
}
RequestOptions options;
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
return !bulkResponse.hasFailures();
}
public Boolean deleteIndex() throws IOException {
GetIndexRequest request2 = new GetIndexRequest(INDEXNAME);
boolean exists = restHighLevelClient.indices().exists(request2,RequestOptions.DEFAULT);
if(exists){
DeleteIndexRequest request = new DeleteIndexRequest(INDEXNAME);
AcknowledgedResponse delete =restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
return delete.isAcknowledged();
}
return false;
}
public List
前端实现
#安装vue和axios cnpm install vue cnpm install axios
将安装目录下的js拷贝到项目
引入js并写相应的javascript方法
搜索按钮绑定事件searchKey,搜索内容绑定keyword
将查询内容展示到页面
根据看狂神说视屏学习的,需要的bilibili搜索狂神说



