Java 爬虫工具Jsoup解析
Jsoup是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址、HTML 文本内容。它提供了一套非常省力的 API,可通过 DOM,CSS 以及类似于 jQuery 的操作方法来取出和操作数据。
jsoup 的主要功能如下:
1. 从一个 URL,文件或字符串中解析 HTML;
2. 使用 DOM 或 CSS 选择器来查找、取出数据;
3. 可操作 HTML 元素、属性、文本;
jsoup 是基于 MIT 协议发布的,可放心使用于商业项目。
jsoup 可以从包括字符串、URL 地址以及本地文件来加载 HTML 文档,并生成 Document 对象实例。
简单而言,Jsoup就是先取html页面代码然后解析这些页面通过Jsoup携带的满足我们绝大多数需求的各种选择器从这个页面中获取我们所需要的重要数据的一款功能强大的html解析器,但也只是相对而言,这里的页面这是死的静态页面,如果你想获取动态生成的页面数据那么你得用到其他的java 爬虫技术,我会不定时更新这些技术一起探讨。下面我们来具体谈谈如何运用Jsoup
一、如何取页面
Jsoup提供了用来解析html页面的方法 parse(),我们通过解析它可以获取整个页面的dom对象,通过这个对象来获取你所需要的页面所须有的参数。获取页面的方法有很多,这里就简单的列举几个:
① 通过Jsoup携带的connect()方法
String htmlPage = Jsoup.connect("https://www.baidu.com").get().toString();
这个方法说需要的参数就是一个String类型的url链接,但是你的注意把这些链接的protrol加上,以免问题,
其实这个方法解决了我们很多问题,我们完全可以把Jsoup解析html抽取成一段通用工具类,然后通过改变拼接的url参数获取到很多我们想要的东西,举个例子:京东和淘宝的商品链接都是固定的,通过改变其三方商品ID来获取商品详情参数。
String url = "https://item.jd.com/11476104681.html";完全可以替换成String url = "https://item.jd.com/"+skuId+".html";
通过改变他的三方商品ID你就可以获取这个页面一些基本数据,像商品的图片和标题什么的都可以轻松获取,而价格因为做了一些相关方面的处理得动态的获取,这里先不做说明,后面慢慢会讲解。
②通过httpclient直接获取这个页面的静态页面
先贴一部分httpclient获取页面工具
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/*** HTTP请求工具类.* @author LuoLong* @since 20150513**/
public class HttpClientUtils {/*** post方式请求.* @param url 请求地址.* @param params 请求参数* @return String*/public static String post(String url, Map<String, String> params) {DefaultHttpClient httpclient = new DefaultHttpClient();String body = null;HttpPost post = postForm(url, params);body = invoke(httpclient, post);httpclient.getConnectionManager().shutdown();return body;}/*** get方式请求.* @param url 请求地址.* @return String*/public static String get(String url) {DefaultHttpClient httpclient = new DefaultHttpClient();String body = null;HttpGet get = new HttpGet(url);body = invoke(httpclient, get);httpclient.getConnectionManager().shutdown();return body;}/*** 请求方法.* @param httpclient DefaultHttpClient.* @param httpost 请求方式.* @return String*/private static String invoke(DefaultHttpClient httpclient,HttpUriRequest httpost) {HttpResponse response = sendRequest(httpclient, httpost);String body = paseResponse(response);return body;}/*** * @param response* @return*/@SuppressWarnings({ "deprecation", "unused" })private static String paseResponse(HttpResponse response) {HttpEntity entity = response.getEntity();String charset = EntityUtils.getContentCharSet(entity);String body = null;try {body = EntityUtils.toString(entity);} catch (ParseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return body;}private static HttpResponse sendRequest(DefaultHttpClient httpclient,HttpUriRequest httpost) {HttpResponse response = null;try {response = httpclient.execute(httpost);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return response;}@SuppressWarnings("deprecation")private static HttpPost postForm(String url, Map<String, String> params) {HttpPost httpost = new HttpPost(url);List<NameValuePair> nvps = new ArrayList<NameValuePair>();Set<String> keySet = params.keySet();for (String key : keySet) {nvps.add(new BasicNameValuePair(key, params.get(key)));}try {httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));} catch (UnsupportedEncodingException e) {e.printStackTrace();}return httpost;}
}
通过get()方法就可以获取html页面的String类型数据
String content = HttpClientUtils.get(url);
或者你可以直接把页面下载到本地,然后解析此html文档获取
File input = new File(FilePath);
Document doc = Jsoup.parse(input, "UTF-8", url);
二、解析页面获取需要的数据
当你获取到页面的dom对象后,那么下面的操作就非常简单了,你只需要通过操作这个dom对象来获取页面所有的静态资源,动态加载的资源不在此列,后面在做讲解。
先贴一段百度网页的源代码:
</form><div id="m"></div></div></div><div id="u"><a class="toindex" href="/">百度首页</a><a href="javascript:;" name="tj_settingicon" class="pf">设置<i class="c-icon c-icon-triangle-down"></i></a><a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" onclick="return false;">登录</a></div><div id="u1"><a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a><a href="http://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a><a href="http://map.baidu.com" name="tj_trmap" class="mnav">地图</a><a href="http://v.baidu.com" name="tj_trvideo" class="mnav">视频</a><a href="http://tieba.baidu.com" name="tj_trtieba" class="mnav">贴吧</a><a href="http://xueshu.baidu.com" name="tj_trxueshu" class="mnav">学术</a><a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" class="lb" onclick="return false;">登录</a><a href="http://www.baidu.com/gaoji/preferences.html" name="tj_settingicon" class="pf">设置</a><a href="http://www.baidu.com/more/" name="tj_briicon" class="bri" style="display: block;">更多产品</a></div></div></div> <div class="s_tab" id="s_tab"> <b>网页</b><a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=" wdfield="word" onmousedown="return c({'fm':'tab','tab':'news'})">新闻</a><a href="http://tieba.baidu.com/f?kw=&fr=wwwt" wdfield="kw" onmousedown="return c({'fm':'tab','tab':'tieba'})">贴吧</a><a href="http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=&fr=wwwt" wdfield="word" onmousedown="return c({'fm':'tab','tab':'zhidao'})">知道</a><a href="http://music.baidu.com/search?fr=ps&ie=utf-8&key=" wdfield="key" onmousedown="return c({'fm':'tab','tab':'music'})">音乐</a><a href="http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=" wdfield="word" onmousedown="return c({'fm':'tab','tab':'pic'})">图片</a><a href="http://v.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&ie=utf-8&word=" wdfield="word" onmousedown="return c({'fm':'tab','tab':'video'})">视频</a><a href="http://map.baidu.com/m?word=&fr=ps01000" wdfield="word" onmousedown="return c({'fm':'tab','tab':'map'})">地图</a><a href="http://wenku.baidu.com/search?word=&lm=0&od=0&ie=utf-8" wdfield="word" onmousedown="return c({'fm':'tab','tab':'wenku'})">文库</a><a href="//www.baidu.com/more/" onmousedown="return c({'fm':'tab','tab':'more'})">更多»</a> </div> <div class="qrcodeCon"> <div id="qrcode"> <div class="qrcode-item qrcode-item-1"> <div class="qrcode-img"></div> <div class="qrcode-text"> <p><b>手机百度</b></p> </div> </div> </div> </div> <div id="ftCon"> <div class="ftCon-Wrapper"><div id="ftConw"><p id="lh"><a id="setf" href="//www.baidu.com/cache/sethelp/help.html" onmousedown="return ns_c({'fm':'behs','tab':'favorites','pos':0})" target="_blank">把百度设为主页</a><a onmousedown="return ns_c({'fm':'behs','tab':'tj_about'})" href="http://home.baidu.com">关于百度</a><a onmousedown="return ns_c({'fm':'behs','tab':'tj_about_en'})" href="http://ir.baidu.com">About Baidu</a><a onmousedown="return ns_c({'fm':'behs','tab':'tj_tuiguang'})" href="http://e.baidu.com/?refer=888">百度推广</a></p><p id="cp">©2017 Baidu <a href="http://www.baidu.com/duty/" onmousedown="return ns_c({'fm':'behs','tab':'tj_duty'})">使用百度前必读</a> <a href="http://jianyi.baidu.com/" class="cp-feedback" onmousedown="return ns_c({'fm':'behs','tab':'tj_homefb'})">意见反馈</a> 京ICP证030173号 <i class="c-icon-icrlogo"></i> <a id="jgwab" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11000002000001">京公网安备11000002000001号</a> <i class="c-icon-jgwablogo"></i></p></div></div></div> <div id="wrapper_wrapper"> </div> </div> <div class="c-tips-container" id="c-tips-container"></div>
在贴上Jsoup自身携带的常用的几个获取dom对象具体元素的方法:
method description
getElementsByClass() 通过Class属性来定位元素,获取的是所有带这个class属性的集合
getElementsByTag(); 通过标签名字来定位元素,获取的是所有带有这个标签名字的元素结合
getElementById(); 通过标签的ID来定位元素,这个是精准定位,因为页面的ID基本不会重复
getElementsByAttributeValue(); 通过属性和属性名来定位元素,获取的也是一个满足条件的集合;
getElementsByAttributeValueMatching() 通过正则匹配属性
比如说我现在要获取百度首页这个title,那么我们得先确定这玩意在哪,通过查看我们发现它是id=”u”的div标签的一个子元素,那么不管那么多我们先通过这个Id取到这个对象然后在获取这个Title,下面是具体操作
//获取页面对象
String startPage="https://www.baidu.com";Document document = Jsoup.connect(startPage).userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36").get();//定位元素父级
Element parentElement = document.getElementById("u");//定位具体元素
Element titleElement = parentElement.getElementsByTag("a").get(0);//获取所需数据
String title = titleElement.text();System.out.println(title);
又或者我需要获取页面《手机百度》这个数据:
String startPage="https://www.baidu.com";Document document = Jsoup.connect(startPage).userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36").get();Element elementById = document.getElementById("qrcode");String text = elementById.getAllElements().get(0).getAllElements().get(1).getElementsByTag("b").text();System.out.println(text);
这就是一个很简单的爬虫编写工具,Jsoup功能很强大,对直接爬取没有动态加载的静态资源页面再适合不过。
Java 爬虫工具Jsoup解析相关推荐
- Java爬虫之JSoup使用教程
title: Java爬虫之JSoup使用教程 date: 2018-12-24 8:00:00 +0800 update: 2018-12-24 8:00:00 +0800 author: me c ...
- java爬虫利器Jsoup的使用
对于长期使用java做编程的程序猿应该知道,java支持的爬虫框架还是有很多的,如:ebMagic.Spider.Jsoup等.今天我们就用Jsoup来实现一个小小的爬虫程序,Jsoup作为kava的 ...
- java爬虫——工具
对于像我这样懒惰的人,治疗的最好方法就是强迫自己做一些事情!有人说过:坚持做一件事情超过21天,就能成为习惯.那么就用写博客这种方式来改变自己的懒惰吧! 今年的10月份气温好像不同于往年,冷的特别早. ...
- 自用Java爬虫工具JAVA-CURL已开源
项目地址: https://github.com/rockswang/... 中央仓库: https://mvnrepository.com/art... 简介 CUrl类是以命令行工具CUrl为参考 ...
- java爬虫框架jsoup
1.java爬虫框架的api jsoup:https://www.open-open.com/jsoup/ 转载于:https://www.cnblogs.com/wuzaipei/p/1057266 ...
- java使用爬虫工具jsoup实现抓取网页的内容及图片并写入到word文档中
背景: 有个朋友最近让帮忙写个小功能,需求大概是,1万个链接,让爬每个链接中的某一段文章并附一张图片,每五个链接写入到一个word文档中. 基本思路,就是先去找个爬虫框架把链接网页中内容和图片写到wo ...
- java爬虫之Jsoup入门
网络爬虫 网络爬虫概念 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁. ...
- 【java爬虫】jsoup爬取网页数据-搜索算法评测/竞品评测
百度抽奖概率改4个小时频繁黑屏频繁弹出源码的前端FE T8李森:请云端高level的同学参加会议...对,我级别到了... 666666 业务背景:如何保证搜索算法的好坏?所以有了竞品评测,自己的AP ...
- JAVA爬虫案例——JSOUP爬取图片并使用v-viewer实现图片预览
前言 网络爬虫是大数据时代收集数据的一种有效手段,合理合法的运用技术手段获取网络数据,实现数据的再利用也是程序员的一项重要技能.本节我们使用java环境下的jsoup实现网络数据的爬取,主要是图片数据 ...
最新文章
- jQuery UI 1.10.1 发布
- Ubuntu 开启telnet、ftp服务
- 阿里云MVP乔帮主:五大类型负载均衡的原理场景详解(文末赠书)
- supervisor linux下进程管理工具
- JAVA bridge设计模式,java设计模式之Bridge
- 深度学习的应用:语音识别、图像理解、自然语言处理
- 推荐一个完美的计算机科学的视频集
- java访问win10共享盘失败_Win10不能访问共享磁盘的解决方法(亲测能用)
- SVN 客户端的安装与配置
- 匿名内部类为什么访问外部类局部变量必须是final的?
- XILINX FPGA数字信号处理——8、FIR滤波器和IIR滤波器原理及实现
- IDEA中格式化代码快捷键
- GATK官方文档翻译--Spark
- 链塔区块链数据平台周报:新增富豪榜、项目点评、收藏等功能 | 链塔数据
- 下载微信公众号中的视频的方法
- 系列推荐 |《最强的 VLC 多媒体开发教程》
- win10连接filco蓝牙键盘
- hdu1677 转换LIS
- China-pub使我出离愤怒
- 我们是如何测试360手机浏览器的 –360手机浏览器测试范围概述