JSOUP 爬虫
作者QQ:1095737364 QQ群:123300273 欢迎加入!
1.mavne 依赖:
<!--html 解析 : jsoup HTML parser library @ http://jsoup.org/-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
2.JSONPUtils工具:
package com.hiione.common.util;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import java.io.IOException;
import java.util.Iterator; public class JsoupUtils { public static String jsoupElement(String content){
Document doc = Jsoup.parse(content);
Element body = doc.body();
Elements aHref=body.select("a");
Elements jsScript = body.select("script");
Elements form = body.select("form");
Elements link = body.select("link");
Elements ifrom = body.select("iframe ");
Elements http = body.select("http");
if(jsScript.size()!=0 ||aHref.size()!=0||form.size()!=0||link.size()!=0||ifrom.size()!=0||http.size()!=0){
return "0";
}
return "";
}
public static String jsoupElementByURL(String content){
String url = "http://as.meituan.com/meishi/all";
Document doc = null;
try {
doc = Jsoup.connect(url).get();
} catch (IOException e1) {
e1.printStackTrace();
}
Element body = doc.body();
Elements aHref=body.select("a");
Elements es=body.select("a");
for (Iterator it = es.iterator(); it.hasNext();) {
Element e = (Element) it.next();
System.out.println(e.text()+" "+e.attr("href"));
}
Elements jsScript = body.select("script");
Elements form = body.select("form");
Elements link = body.select("link");
Elements ifrom = body.select("iframe ");
Elements http = body.select("http");
if(jsScript.size()!=0 ||aHref.size()!=0||form.size()!=0||link.size()!=0||ifrom.size()!=0||http.size()!=0){
return "0";
}
return "";
}
}
3.jsoup 简介

4.文档输入
// 直接从字符串中输入 HTML 文档
String html = "<html><head><title>JSONP</title></head>" +
"<body><p>这里是 jsoup 项目的相关文章</p></body></html>";
Document doc = Jsoup.parse(html);
// 从URL直接加载 HTML 文档
Document doc = Jsoup.connect("http://www.baidu.net/").get();
String title = doc.title();
Document doc = Jsoup.connect("http://www.baidu.net/")
.data("query", "Java") //请求参数
.userAgent("I’m jsoup") //设置User-Agent
.cookie("auth", "token") //设置cookie
.timeout(3000) //设置连接超时时间
.post(); //使用POST方法访问URL
// 从文件中加载 HTML 文档
File input = new File("D:/test.html");
Document doc = Jsoup.parse(input,"UTF-8","http://www.oschina.net/");
5.解析并提取 HTML 元素
File input = new File("D:/test.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://www.baidu.net/");
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
File input = new File("D:\test.html");
Document doc = Jsoup.parse(input,"UTF-8","http://www.baidu.net/");
Elements links = doc.select("a[href]"); // 具有 href 属性的链接
Elements pngs = doc.select("img[src$=.png]");//所有引用png图片的元素
Element masthead = doc.select("div.masthead").first();
// 找出定义了 class="masthead" 的元素
Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
6.修改数据
doc.select("div.comments a").attr("rel", "nofollow");
//为所有链接增加 rel=nofollow 属性
doc.select("div.comments a").addClass("mylinkclass");
//为所有链接增加 class="mylinkclass" 属性
doc.select("img").removeAttr("onclick"); //删除所有图片的onclick属性
doc.select("input[type=text]").val(""); //清空所有文本输入框中的文本
7.HTML 文档清理
String unsafe = "<p><a href='http://www.oschina.net/' onclick='stealCookies()'>JSONP</a></p>";
String safe = Jsoup.clean(unsafe, Whitelist.basic());
// 输出:
// <p><a href="http://www.baidu.net/" rel="nofollow">JSONP</a></p>

8.jsoup 的过人之处——选择器



JSOUP 爬虫的更多相关文章
- jsoup爬虫简书首页数据做个小Demo
代码地址如下:http://www.demodashi.com/demo/11643.html 昨天LZ去面试,遇到一个大牛,被血虐一番,发现自己基础还是很薄弱,对java一些原理掌握的还是不够稳固, ...
- (java)Jsoup爬虫学习--获取智联招聘(老网站)的全国java职位信息,爬取10页
Jsoup爬虫学习--获取智联招聘(老网站)的全国java职位信息,爬取10页,输出 职位名称*****公司名称*****职位月薪*****工作地点*****发布日期 import java.io.I ...
- (java)Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息
Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息 此例将页面图片和url全部输出,重点不太明确,可根据自己的需要输出和截取: import org.jsoup.Jsou ...
- 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo
简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...
- JSOUP爬虫示例
利用JSOUP做爬虫,爬取我博客中的所有标题加链接,代码示例如下: package com.test.jsoup; import java.io.IOException; import org.jso ...
- HttpClient&Jsoup爬虫的简单应用
详细的介绍已经有很多前辈总结,引用一下该篇文章:https://blog.csdn.net/zhuwukai/article/details/78644484 下面是一个代码的示例: package ...
- Jsoup爬虫任务总结
这两周由于公司需要大量数据爬取进数据库给用户展示素材,在不停的做爬虫工作,现在总算基本完成就剩清理数据的工作: 公司有一个采集器管理后台的项目,可以直接把爬虫代码打包成jar导入进去设置定时参数即可: ...
- 利用jsoup爬虫工具,爬取数据,并利用excel导出
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...
- Jsoup爬虫解析
需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...
- jsoup爬虫,项目实战,欢迎收看
import com.mongodb.BasicDBObject import com.mongodb.DBCollection import org.jsoup.Jsoup import org.j ...
随机推荐
- 转 C#关于DateTime得到的当前时间的格式和用法
DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.T ...
- 【转】MarkDown添加图片的三种方式
原文:https://www.jianshu.com/p/280c6a6f2594 ----------------------------------------------------- 插图最基 ...
- markdown种嵌入html标签,实现自定义样式
转:https://www.cnblogs.com/buwuliao/p/9578918.html -------------------------------------------------- ...
- 后台将数据传回前台的三种绑定的方式(Model,Map.ModelAndView)
//方式1:通过model 将数据绑定 @RequestMapping(value = "findByIdModel", method = RequestMethod.GET) p ...
- 题解 洛谷P1281 【书的复制】
蒟蒻的\(DP\)很菜,\(SO\)我准备上一套二分的玄学操作 一.简单的二分答案 二分主要是用来解决一些最值问题,它可以有效的优化暴力,使复杂度减少到\(O(logn)\). 我先给大家介绍一下二分 ...
- 六.搭建基本的Web服务
1.安装httpd软件包 ]# yum -y install httpd 2.重起httpd服务 ]# systemctl restart httpd ]# systemctl enable http ...
- asp.net之大文件断点续传
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- C二维字符数组的使用及如何获取二维数组的总行数和总列数!
#include <stdio.h> #include <stdlib.h> int main(){ char str[][30] = {"zhangsan" ...
- Subspace Subcode
子码(subcode)的概念来自信息编码,不太容易理解.通常是子域编码(subfield subcode),也可以扩展到子空间编码(subspace subcode). 子空间或者子域编码的一个基本想 ...
- php之简单socket编程
一.什么是套接字 源IP地址和目的IP地址以及源端口号和目的端口号的组合称为套接字.其用于标识客户端请求的服务器和服务. 二.php套接字实例 服务端代码:socket.php <?php er ...