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 ...
随机推荐
- 结构型模式(二) 桥接模式(Bridge)
一.动机(Motivation) 在很多游戏场景中,会有这样的情况:[装备]本身会有的自己固有的逻辑,比如枪支,会有型号的问题,同时现在很多的游戏又在不同的介质平台上运行和使用,这样就使得游戏的[装备 ...
- sql注入和防sql注入
sql注入: from pymysql import * def main(): # 创建连接 conn = connect(host="127.0.0.1", port=3306 ...
- tcp的三次握手和四次挥手转自https://www.jianshu.com/p/d3725391af59
三次握手(three-way handshaking) 1.背景:TCP位于传输层,作用是提供可靠的字节流服务,为了准确无误地将数据送达目的地,TCP协议采纳三次握手策略. 2.原理: 1)发送端首先 ...
- tensorflow API _ 1 (control_flow_ops.cond)
该函数用来控制程序执行流,相当于if-else了import tensorflow as tffrom tensorflow.python.ops import control_flow_ops a ...
- C位运算符的使用
#include <stdio.h> int main(void) { //位运算符 & | ^ ~ printf("8|2=%d\n",8|2);// 10 ...
- LibreOJ #526. 「LibreOJ β Round #4」子集
二次联通门 : LibreOJ #526. 「LibreOJ β Round #4」子集 /* LibreOJ #526. 「LibreOJ β Round #4」子集 考虑一下,若两个数奇偶性相同 ...
- Comet OJ Contest #13 简要题解
C2 首先用并查集维护\(1\)的连通块,然后用另外一个并查集维护第\(i\)行中,第\(j\)列之后的第一个\(0\)的位置,就是如果当前位置是\(1\)那么它的父亲是它右边的格子,否则是它自己. ...
- 记录一个webstorm的设置或者说小技巧
在 html 的元素中,如果输入属性,默认会填充 引号,在 react 书写中非常不方便. 其中的JSX很多时候是不需要 quotation 的,只是需要一个 括号 {} 即可. 自己找了下webst ...
- CSP-S 复赛之前的任务计划
一. 最短路算法复习 ★1.Dijkstra: 2. SPFA: 3. Floyd: 二. DP 复习 ★1.背包问题: 2.区间 DP: 3.状压 DP: 三. 数据结构 ★1. 线段树: 2. 树 ...
- Spring的Core模块
Core模块主要的功能是实现了反向控制IOC(Inversion of Control)与依赖注入DI(Dependency Injection).Bean配置以及加载.Core模块中有Beans.B ...