前言:
  时下互联网第一波的浪潮已消逝,随着而来的基于万千数据的物联网时代,因而数据成为企业的重要战略资源之一。基于数据抓取技术,本文介绍了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.</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方法的抓取方式,敬请期待!

Java实现多种方式的http数据抓取的更多相关文章

  1. java中多种方式读文件

    转自:http://www.jb51.net/article/16396.htm java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内 ...

  2. 爬虫---selenium动态网页数据抓取

    动态网页数据抓取 什么是AJAX: AJAX(Asynchronouse JavaScript And XML)异步JavaScript和XML.过在后台与服务器进行少量数据交换,Ajax 可以使网页 ...

  3. [原创.数据可视化系列之十二]使用 nodejs通过async await建立同步数据抓取

    做数据分析和可视化工作,最重要的一点就是数据抓取工作,之前使用Java和python都做过简单的数据抓取,感觉用的很不顺手. 后来用nodejs发现非常不错,通过js就可以进行数据抓取工作,类似jqu ...

  4. 网页数据抓取工具,webscraper 最简单的数据抓取教程,人人都用得上

    Web Scraper 是一款免费的,适用于普通用户(不需要专业 IT 技术的)的爬虫工具,可以方便的通过鼠标和简单配置获取你所想要数据.例如知乎回答列表.微博热门.微博评论.淘宝.天猫.亚马逊等电商 ...

  5. 大数据抓取采集框架(摘抄至http://blog.jobbole.com/46673/)

    摘抄至http://blog.jobbole.com/46673/ 随着BIG DATA大数据概念逐渐升温,如何搭建一个能够采集海量数据的架构体系摆在大家眼前.如何能够做到所见即所得的无阻拦式采集.如 ...

  6. Phantomjs+Nodejs+Mysql数据抓取(1.数据抓取)

    概要: 这篇博文主要讲一下如何使用Phantomjs进行数据抓取,这里面抓的网站是太平洋电脑网估价的内容.主要是对电脑笔记本以及他们的属性进行抓取,然后在使用nodejs进行下载图片和插入数据库操作. ...

  7. 数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置

     数据抓取的艺术(一):Selenium+Phantomjs数据抓取环境配置 2013-05-15 15:08:14 分类: Python/Ruby     数据抓取是一门艺术,和其他软件不同,世界上 ...

  8. python爬虫数据抓取方法汇总

    概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...

  9. [nodejs,expressjs,angularjs2] LOL英雄列表数据抓取及查询显示应用

    新手练习,尝试使用angularjs2 [angularjs2 数据绑定,监听数据变化自动修改相应dom值,非常方便好用,但与传统js(jquery)的使用方法会很不同,Dom操作也不太习惯] 应用效 ...

随机推荐

  1. Linux安装MySQL

    步骤: 1 [qq@localhost Desktop]$ su root    //以root身份进入 Password:(默认为空) 2 [root@localhost Desktop]# yum ...

  2. java中关键字this的使用

    在团队代码中看到对于当前类中的方法,使用了this关键字.经过测试发现,在此种情况下,this关键字的使用可有可无.因此,对java中this的使用做下总结: package testTHIS; pu ...

  3. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  4. 子div设置浮动无法把父div撑开。

    <div class="mainBox"> <div class="leftBox"></div> <div clas ...

  5. [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)

    原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...

  6. 基于位图(Bitmap、BitmapData)的图片处理方法(C#)

    目前操作位图的主流方法有三种: 1.基于Bitmap像素的处理方法,以GetPixel()和SetPixel()方法为主.方法调用简单,但是效率偏低. 2.基于内存的像素操作方法,以System.Ru ...

  7. Titanium.UI.createAlertDialog

    学习TI 记录一下 1.确认对话框 Js代码 var a = Titanium.UI.createAlertDialog({ title:'添加人员信息', message:"人员添加成功& ...

  8. android 启动模式介绍

    Android启动模式 (1)Task:与Android系统是个多任务的系统中的任务是不同的.后者更倾向于多进程和多线程来说的,而这里的任务与application(应用程序)和activity(活动 ...

  9. webView 自适应高度 document.body 属性

    前段时间开发遇到webView 高度自适应问题,用最初的方法无效,找了些资料,记录下. 1.若网页中含有< !DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  10. [MySQL Reference Manual] 23 Performance Schema结构

    23 MySQL Performance Schema 23 MySQL Performance Schema 23.1 性能框架快速启动 23.2 性能框架配置 23.2.1 性能框架编译时配置 2 ...