Jsoup查找dom元素
package com.open1111.jsoup;
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;
public class Demo02 {//
public static void main(String[] args) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httpclient实例
HttpGet httpget = new HttpGet("http://www.cnblogs.com/"); // 创建httpget实例
CloseableHttpResponse response = httpclient.execute(httpget); // 执行get请求
HttpEntity entity=response.getEntity(); // 获取返回实体
String content=EntityUtils.toString(entity, "utf-8");//设置content编码
response.close(); // 关闭流和释放系统资源
Document doc=Jsoup.parse(content); // 解析网页 得到文档对象
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);
Elements itemElements=doc.getElementsByClass("post_item"); // 根据样式名称来查询DOM
System.out.println("=======输出post_item==============");
for(Element e:itemElements){//for加强循环
System.out.println(e.html());
System.out.println("-------------");
}
Elements widthElements=doc.getElementsByAttribute("width"); // 根据属性名称来查询DOM
System.out.println("=======输出with的DOM==============");
for(Element e:widthElements){
System.out.println(e.toString());
System.out.println("-------------");
}
Elements targetElements=doc.getElementsByAttributeValue("target", "_blank");//根据属性名和属性值来获取dom元素
System.out.println("=======输出target-_blank的DOM==============");
for(Element e:targetElements){
System.out.println(e.toString());
System.out.println("-------------");
}
}
}
Jsoup查找dom元素的更多相关文章
- Jsoup(二)-- Jsoup查找DOM元素
一.Jsoup查找DOM元素的方法 getElementById(String id) 根据id 来查询DOM getElementsByTag(String tagName) 根据tag 名称来查询 ...
- (二)Jsoup 查找 DOM 元素
第一节: Jsoup 查找 DOM 元素 getElementById(String id) 根据 id 来查询 DOM getElementsByTag(String tagName) 根据 tag ...
- (三)Jsoup 使用选择器语法查找 DOM 元素
第一节: Jsoup 使用选择器语法查找 DOM 元素 Jsoup使用选择器语法查找DOM元素 我们前面通过标签名,Id,Class样式等来搜索DOM,这些是不能满足实际开发需求的, 很多时候我们需要 ...
- (四)Jsoup 获取 DOM 元素属性值
第一节: Jsoup 获取 DOM 元素属性值 Jsoup获取DOM元素属性值 比如我们要获取博客的href属性值: 我们这时候就要用到Jsoup来获取属性的值 : 我们给下示例代码: package ...
- Jsoup(三)-- Jsoup使用选择器语法查找DOM元素
1.Jsoup可以使用类似于CSS或jQuery的语法来查找和操作元素. 2.实例如下: public static void main(String[] args) throws Exception ...
- Jsoup获取DOM元素
(1)doc.getElementsByTag(String tagName); (2)doc.getElementById(String id); (3)doc.getElementsByClass ...
- Jsoup(四)-- Jsoup获取DOM元素属性值
1.获取博客园的博客标题以及博客地址,获取友情链接 2.代码实现: public static void main(String[] args) throws Exception{ // 创建http ...
- jQuery常用的查找Dom元素方法
废话不多说,先来个总结,然后下面是demo 一. 同级节点之间的检索(检索深度N=0) next()是在兄弟节点中,往后匹配; prev()是在兄弟节点中,往前匹配. 二. 父级/子级节点的检索(检索 ...
- jQuery使用(四):DOM操作之查找兄弟元素和父级元素
查找兄弟元素 向下查找兄弟元素 next() nextAll() nextUntil() 向上查找兄弟元素 prev() prevAll() prevUntil() 查找所有兄弟元素 siblings ...
随机推荐
- Saving output of a grep into a file with colors
19 down vote favorite 7 I need to save the result of a grep command into a file, but I also want the ...
- C#----接口的显式实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...
- 蚂蚁金服HR电话面
1.先自我介绍一下. 2.问到项目问题.(项目介绍,用到哪些技术,如何测试的.) 3.一些基础知识. a) list.map.set是继承了collection的吗? List:1.可以允许重复的对 ...
- [Windows]获取系统版本号
1 string GetMainProgInfo() 2 { 3 string strRet; 4 TCHAR szPath[MAX_PATH]; 5 GetModuleFileName(NULL,s ...
- 初识Composer
关于vendor name和project name的区别? 包名(package name)包含了供应商名(vendor name)和项目名(project name)是为了避免命名冲突的 requ ...
- 洛谷P3694 邦邦的大合唱站队/签到题
P3694 邦邦的大合唱站队/签到题 题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶 ...
- 2017-10-6 清北刷题冲刺班a.m
1.角谷猜想 #include<iostream> #include<cstdio> #include<cstring> #define maxn 10010 us ...
- 洛谷 P1551 亲戚(并查集模板)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P1551 思路: 很显然地我们会发现,这是一道并查集的模板题,并且是考察了并查集中的”并“和”查“的操 ...
- git教程1-gitlab部署
https://about.gitlab.com/install/#centos-7 https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/ gitla ...
- avro-maven-plugin
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...