http数据抓取Java实现的几种方式
前言:
时下互联网第一波的浪潮已消逝,随着而来的基于万千数据的物联网时代,因而数据成为企业的重要战略资源之一。基于数据抓取技术,本文介绍了java相关抓取工具,并附上demo源码供感兴趣的朋友测试!
1)JDK自带HTTP连接,获取页面或Json
2) JDK自带URL连接,获取页面或Json
3)HttpClient Get工具,获取页面或Json
4)commons-io工具,获取页面或Json
5) Jsoup工具(通常用于html字段解析),获取页面,非Json返回格式】
完整代码:
package com.yeezhao.common.http;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.io.IOUtils;import org.jsoup.Jsoup;/** * http工具对比 * * @author Administrator -> junhong * * 2016年12月27日 */public class HttpFetchUtil { /** * 获取访问的状态码 * @param request * @return * @throws Exception */ public static int getResponseCode(String request) throws Exception { URL url = new URL(request); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); return conn.getResponseCode(); } /** * 1)JDK自带HTTP连接,获取页面或Json * @param request * @param charset * @return * @throws Exception */ public static String JDKFetch(String request, String charset) throws Exception { URL url = new URL(request); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //模拟浏览器参数 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + " (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream input = conn.getInputStream(); StringBuffer sb = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset)); String s; while ((s = reader.readLine()) != null) { sb.append(s + "\n"); } input.close(); conn.disconnect(); return sb.toString(); } return ""; } /** * 2) JDK自带URL连接,获取页面或Json * @param request * @param charset * @return * @throws Exception */ public static String URLFetch(String request, String charset) throws Exception { URL url = new URL(request); return IOUtils.toString(url.openStream()); } /** * 3)HttpClient Get工具,获取页面或Json * @param url * @param charset * @return * @throws Exception */ public static String httpClientFetch(String url, String charset) throws Exception { // GET HttpClient httpClient = new HttpClient(); httpClient.getParams().setContentCharset(charset); HttpMethod method = new GetMethod(url); httpClient.executeMethod(method); return method.getResponseBodyAsString(); } /** * 4)commons-io工具,获取页面或Json * @param url * @param charset * @return * @throws Exception */ public static String commonsIOFetch(String url, String charset) throws Exception { return IOUtils.toString(new URL(url), charset); } /** * 5) Jsoup工具(通常用于html字段解析),获取页面,非Json返回格式 * @param url * @return * @throws Exception */ public static String jsoupFetch(String url) throws Exception { return Jsoup.parse(new URL(url), 2 * 1000).html(); } }
测试代码:
package com.yeezhao.common.http;import org.junit.After;import org.junit.Before;import org.junit.Test;/** * 测试类 * 3个测试链接: * 1)百科网页 * 2)浏览器模拟获取接口数据 * 3)获取普通接口数据 * @author Administrator -> junhong * * 2016年12月27日 */public class HttpFetchUtilTest { String seeds[] = {"http://baike.baidu.com/view/1.htm","http://m.ximalaya.com/tracks/26096131.json","http://remyapi.yeezhao.com/api/query?wd=%E5%91%A8%E6%98%9F%E9%A9%B0%E7%9A%84%E7%94%B5%E5%BD%B1"}; final static String DEFAULT_CHARSET = "UTF-8"; @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { System.out.println("--- down ---"); } @Test public void testGetResponseCode() throws Exception{ for(String seed:seeds){ int responseCode = HttpFetchUtil.getResponseCode(seed); System.out.println("ret="+responseCode); } } @Test public void testJDKFetch() throws Exception{ for(String seed:seeds){ String ret = HttpFetchUtil.JDKFetch(seed, DEFAULT_CHARSET); System.out.println("ret="+ret); } } @Test public void testURLFetch() throws Exception{ for(String seed:seeds){ String ret = HttpFetchUtil.URLFetch(seed, DEFAULT_CHARSET); System.out.println("ret="+ret); } } @Test public void testHttpClientFetch()throws Exception { for(String seed:seeds){ String ret = HttpFetchUtil.httpClientFetch(seed, DEFAULT_CHARSET); System.out.println("ret="+ret); } } @Test public void testCommonsIOFetch()throws Exception { for(String seed:seeds){ String ret = HttpFetchUtil.commonsIOFetch(seed, DEFAULT_CHARSET); System.out.println("ret="+ret); } } @Test public void testJsoupFetch() throws Exception{ for(String seed:seeds){ String ret = HttpFetchUtil.jsoupFetch(seed); System.out.println("ret="+ret); } } }
附:相关jar依赖
...<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.7.3</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>...
后语:
现在的数据时代,有着"数据即财富"的理念。因此,数据抓取技术将一直发展更新,基于此后续还将扩充针对POST方法的抓取方式,敬请期待!
以上是http数据抓取Java实现的几种方式的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
