ps: 虽然使用jsoup可以代替HttpClient 直接发起请求解析数据,但往往不会这样用,因为实际的开发过程中,需要使用到多线程,连接池,代理等等方式,而jsoup 对这些的支持并不是很好,所以我们一直把jsoup仅仅作为Html解析工具。
================================================================================================
(1)
1.1 我们抓取到页面之后,还需要对页面进行解析。可以是用字符串处理工具解析页面,也可以使用正则表达式,但这些方法都会带来很大的成本,。
(2) Jsoup
2.1 jsoup 是一款 java 的 HTML 的解析器,可直接解析某个URL的地址,HTML 文本内容 ,它提供了一个非常省力的API,可通过 DOM,CSS 以及类似于JQuery 的操作方法来取出和操作数据。
jsoup的主要功能
1.从一个url,文件或者字符串中解析 HTML
2.使用DOS或者CSS选择器来查找,取出数据;
3.可操作HTML元素,属性,文本。
2.2 使用 IDEA 工具需要坐标(jar)
Jsoup 1.10.2 , junit , commons-io , commons-lang3.
public void test1() throws Exception{
//解析url地址,第一个参数访问的url,第二个参数是访问时候的超时时间。
Document doc = Jsoup.parse(new URL("http://www.itheima.com"),1000);
String title = doc.getElementsByTag("title").first().text();
System.out.println(title);
}
(3)可以使用Selector选择器,获取数据。
Document doc = Jsoup.parse(new URL("http://www.itheima.com"),1000);
Element span = doc.select("span").first();
System.out.println(span);
#id , .class通过class名称查找元素。 [attribute] :利用属性查找元素。 [attr=value] 利用属性值来查找元素。
Document doc = Jsoup.parse(new URL("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=bfd3f7e00bd840bd9b15f334678118c9"), 1000);
根据id查询元素
// Element element = doc.getElementById("J_scroll_loading");
根据标签查询元素
//Elements element1 = doc.getElementsByTag("span");
根据class获取属性
// Element element1 = doc.getElementsByClass("fg-line-value").first();
根据属性获取元素
Element element1 = doc.getElementsByAttribute("searchlog(1,0,0,62,'华为')").first();
System.out.println(element1);
Document doc = Jsoup.parse(new URL("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=bfd3f7e00bd840bd9b15f334678118c9"), 1000);
Element e = doc.getElementById("J_goodsList");
String str = "";
从元素中获取id
str = e.id();
从元素中获取className
// str = e.className();
从元素中的属性attr
// str = e.attr("id");
从元素中获取所有属性attributes。
// Attributes strd = e.attributes();
从元素中获取文本内容
str = e.text();
System.out.println(str);
3.选择器selector
Document doc = Jsoup.parse(new URL("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=bfd3f7e00bd840bd9b15f334678118c9"), 1000);
//tagname:通过标签查找元素,比如:span
// Elements elements = doc.select("span");
// for (Element e : elements) {
// System.out.println(e);
// }
//通过id查找元素,例如:#city_jd
// Elements e = doc.select("#J_goodsList");
//通过class名称查询元素 例如:.class_a
// Element e = doc.select(".gl-item").first();
//利用属性查找元素,例如[gl-item]
// Elements e = doc.select("[src]");
//利用属性和属性的值查找元素
Elements e = doc.select("[class=gl-item]");
System.out.println(e);
6. Document doc = Jsoup.parse(new URL("https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&wq=%E6%89%8B%E6%9C%BA&pvid=bfd3f7e00bd840bd9b15f334678118c9"), 1000);
el#id:元素+id
Elements e = doc.select("li#J_goodsList");
el.class : 元素+class 例如:li.class_q
Elements e = doc.select("li.gl-item");
el[attr] 元素+ 属性名 比如: span [abc]
任意组合: 比如 span[abc].s_name
ancestor child: 查找某元素下子元素,比如: .city_con li ,查找"city_con"下的所有li
parent > child : 查找某父类元素下的直接子元素,比如
.city_con > ul > li 查找city_con第一级(直接子元素)的ul,再找所有ul下的第一级li
parent >* : 查找某个父类元素下所有直接子元素
Jsoup用法
最新推荐文章于 2024-05-03 17:19:41 发布