我们前面通过标签名,Id,Class样式等来搜索DOM,这些是不能满足实际开发需求的,很多时候我们需要寻找有规律的DOM集合,很多个有规律的标签层次,这时候,选择器就用上了。css jquery 都有,Jsoup支持css,jquery类似的选择器语法。
/**
* 有层级关系
*/
@Test
public void test3() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements linkElements = doc.select("#post_list .post_item .post_item_body h3 a"); //通过选择器查找所有博客链接DOM(范围重小到大)
for (Element e : linkElements) {
System.out.println("博客标题:" + e.text());//超链接的内容
}
System.out.println("--------------------带有href属性的a元素--------------------------------");
Elements hrefElements = doc.select("a[href]"); // 带有href属性的a元素
for (Element e : hrefElements) {
System.out.println(e.toString());
}
System.out.println("------------------------查找扩展名为.png的图片----------------------------");
Elements imgElements = doc.select("img[src$=.png]"); // 查找扩展名为.png的图片DOM节点
for (Element e : imgElements) {
System.out.println(e.toString());
}
System.out.println("------------------------获取第一个元素----------------------------");
Element element = doc.getElementsByTag("title").first(); // 获取tag是title的所有DOM元素
String title = element.text(); // 返回元素的文本
System.out.println("网页标题是:" + title);
}

五、Jsoup获取DOM元素属性值


/**
* 获取 DOM 元素属性值
*/
@Test
public void test4() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements linkElements = doc.select("#post_list .post_item .post_item_body h3 a"); //通过选择器查找所有博客链接DOM
for (Element e : linkElements) {
System.out.println("博客标题:" + e.text());//获取里面所有的文本
System.out.println("博客地址:" + e.attr("href"));
System.out.println("target:" + e.attr("target"));
}
System.out.println("------------------------友情链接----------------------------");
Element linkElement = doc.select("#friend_link").first();
System.out.println("纯文本:" + linkElement.text());//去掉 html
System.out.println("------------------------Html----------------------------");
System.out.println("Html:" + linkElement.html());
}
/**
*/
@Test
public void test5() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements linkElements = doc.select("#post_list .post_item .post_item_body h3 a"); //通过选择器查找所有博客链接DOM
for (Element e : linkElements) {
System.out.println(e.attr("href"));
}
}
注意:Element 的几个获取内容的方法区别
1. text()? ? ? ? ? ? 获取的是去掉了 html 元素,也就是只用元素内容
2. toString()? ? ??DOM
3. html()? ? ? ? ? 获取里面所有的 html 包括文本
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import java.io.IOException;
public class Main {
/**
* 输入一个网址返回这个网址的字符串
*/
public String getHtml(String str) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httpclient实例
HttpGet httpget = new HttpGet(str); // 创建httpget实例
CloseableHttpResponse response = httpclient.execute(httpget); // 执行get请求
HttpEntity entity = response.getEntity(); // 获取返回实体
String content = EntityUtils.toString(entity, "utf-8");
response.close(); // 关闭流和释放系统资源
return content;
}
/**
* 爬取 博客园
* 1、网页标题
* 2、口号
*/
@Test
public void test() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements elements = doc.getElementsByTag("title"); // 获取tag是title的所有DOM元素
Element element = elements.get(0); // 获取第1个元素
String title = element.text(); // 返回元素的文本
System.out.println("网页标题:" + title);
Element element2 = doc.getElementById("site_nav_top"); // 获取id=site_nav_top的DOM元素
String navTop = element2.text(); // 返回元素的文本
System.out.println("口号:" + navTop);
}
/**
* Jsoup 查找 DOM 元素
*/
@Test
public void test2() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements itemElements = doc.getElementsByClass("post_item"); // 根据样式名称来查询DOM
System.out.println("=======输出post_item==============");
for (Element e : itemElements) {
System.out.println(e.html());//获取里面所有的 html 包括文本
System.out.println("\n");
}
Elements widthElements = doc.getElementsByAttribute("width"); // 根据属性名称来查询DOM(id class type 等),用的少一般很难找用这种方法
System.out.println("=======输出with的DOM==============");
for (Element e : widthElements) {
System.out.println(e.toString());//不能用 e.html() 这里需要输出 DOM
}
//<a href="https://www.cnblogs.com/rope/" target="_blank"><img width="48" height="48" class="pfs" src="//pic.cnblogs.com/face/1491596/20180916131220.png" alt=""/></a>
Elements targetElements = doc.getElementsByAttributeValue("target", "_blank");
System.out.println("=======输出target-_blank的DOM==============");
for (Element e : targetElements) {
System.out.println(e.toString());
}
}
/**
* 有层级关系
*/
@Test
public void test3() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements linkElements = doc.select("#post_list .post_item .post_item_body h3 a"); //通过选择器查找所有博客链接DOM(范围重小到大)
for (Element e : linkElements) {
System.out.println("博客标题:" + e.text());//超链接的内容
}
System.out.println("--------------------带有href属性的a元素--------------------------------");
Elements hrefElements = doc.select("a[href]"); // 带有href属性的a元素
for (Element e : hrefElements) {
System.out.println(e.toString());
}
System.out.println("------------------------查找扩展名为.png的图片----------------------------");
Elements imgElements = doc.select("img[src$=.png]"); // 查找扩展名为.png的图片DOM节点
for (Element e : imgElements) {
System.out.println(e.toString());
}
System.out.println("------------------------获取第一个元素----------------------------");
Element element = doc.getElementsByTag("title").first(); // 获取tag是title的所有DOM元素
String title = element.text(); // 返回元素的文本
System.out.println("网页标题是:" + title);
}
/**
* 获取 DOM 元素属性值
*/
@Test
public void test4() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements linkElements = doc.select("#post_list .post_item .post_item_body h3 a"); //通过选择器查找所有博客链接DOM
for (Element e : linkElements) {
System.out.println("博客标题:" + e.text());//获取里面所有的文本
System.out.println("博客地址:" + e.attr("href"));
System.out.println("target:" + e.attr("target"));
}
System.out.println("------------------------友情链接----------------------------");
Element linkElement = doc.select("#friend_link").first();
System.out.println("纯文本:" + linkElement.text());//去掉 html
System.out.println("------------------------Html----------------------------");
System.out.println("Html:" + linkElement.html());
}
/**
* 获取文章的 url
*/
@Test
public void test5() throws IOException {
Document doc = Jsoup.parse(getHtml("http://www.cnblogs.com/")); // 解析网页 得到文档对象
Elements linkElements = doc.select("#post_list .post_item .post_item_body h3 a"); //通过选择器查找所有博客链接DOM
for (Element e : linkElements) {
System.out.println(e.attr("href"));
}
}
}
六、Jsoup工具类
----------
public class JsoupUtil {
/**
* 获取value值
*
* @param e
* @return
*/
public static String getValue(Element e) {
return e.attr("value");
}
/**
* 获取
* <tr>
* 和
* </tr>
* 之间的文本
*
* @param e
* @return
*/
public static String getText(Element e) {
return e.text();
}
/**
* 识别属性id的标签,一般一个html页面id唯一
*
* @param body
* @param id
* @return
*/
public static Element getID(String body, String id) {
Document doc = Jsoup.parse(body);
// 所有#id的标签
Elements elements = doc.select("#" + id);
// 返回第一个
return elements.first();
}
/**
* 识别属性class的标签
*
* @param body
* @param class
* @return
*/
public static Elements getClassTag(String body, String classTag) {
Document doc = Jsoup.parse(body);
// 所有#id的标签
return doc.select("." + classTag);
}
/**
* 获取tr标签元素组
*
* @param e
* @return
*/
public static Elements getTR(Element e) {
return e.getElementsByTag("tr");
}
/**
* 获取td标签元素组
*
* @param e
* @return
*/
public static Elements getTD(Element e) {
return e.getElementsByTag("td");
}
最后
毕竟工作也这么久了 ,除了途虎一轮,也七七八八面试了不少大厂,像阿里、饿了么、美团、滴滴这些面试过程就不一一写在这篇文章上了。我会整理一份详细的面试过程及大家想知道的一些问题细节
美团面试经验
 字节面试经验  菜鸟面试经验  蚂蚁金服面试经验  唯品会面试经验 
因篇幅有限,图文无法详细发出,感兴趣的朋友可以点击这里前往我的腾讯文档免费获取上述资料!
r");
}
/**
* 获取td标签元素组
*
* @param e
* @return
*/
public static Elements getTD(Element e) {
return e.getElementsByTag("td");
}
最后
毕竟工作也这么久了 ,除了途虎一轮,也七七八八面试了不少大厂,像阿里、饿了么、美团、滴滴这些面试过程就不一一写在这篇文章上了。我会整理一份详细的面试过程及大家想知道的一些问题细节
美团面试经验
[外链图片转存中…(img-Aspw2TxR-1628069138673)] 字节面试经验 [外链图片转存中…(img-uOWjJ7zy-1628069138675)] 菜鸟面试经验 [外链图片转存中…(img-FaqEmttF-1628069138677)] 蚂蚁金服面试经验 [外链图片转存中…(img-0QNmf843-1628069138679)] 唯品会面试经验 [外链图片转存中…(img-ihXslCe1-1628069138681)]
因篇幅有限,图文无法详细发出,感兴趣的朋友可以点击这里前往我的腾讯文档免费获取上述资料!
|