Jsoup简单操作
Jsoup网页数据抓取
中文使用手册:https://www.open-open.com/jsoup/
一、Jsoup是什么?
Jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据。
二、使用步骤
1、pom依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
2、工具类使用
新建Content对象,通过下方返回的数据进行下一波逻辑操作
private List<Content> parseUtil(String keyword) throws Exception { String url="https://search.jd.com/Search?keyword="+keyword; Document document=Jsoup.parse(new URL(url),30000); Element element = document.getElementById("J_goodsList"); //System.out.println(element.html()); Elements li = element.getElementsByTag("li"); List<Content> list=new ArrayList <>(); for(Element e : li) { String img = e.getElementsByTag("img").eq(0).attr("src"); String price = e.getElementsByClass("p-price").eq(0).text(); String title = e.getElementsByClass("p-name").eq(0).text(); list.add(new Content(img,price,title)); } return list; }
浙公网安备 33010602011771号