java代码采集 采集器代码

如何用Java实现数据采集?

Java网页数据采集器例子如下:

成都创新互联公司成立与2013年,是专业互联网技术服务公司,拥有项目成都做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元随州做网站,已为上家服务,为随州各地企业和个人服务,联系电话:18982081108

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* Group 类 用于匹配和抓取 html页面的数据

*/

public class Group {

public static void main(String[] args) {

// Pattern 用于编译正则 这里用到了3个正则 分别用括号()包住

// 第1个正则用于匹配URL 当然这里的正则不一定准确 这个匹配URL的正则就是错误的 只是在这里刚好能匹配出来

// 第2个正则是用于匹配标题 SoFlash的

// 第3个正则用于匹配日期

/* 这里只用了一条语句便把url,标题和日期全部给匹配出来了 */

Pattern p = Pattern

.compile("='(\\w.+)'(\\w.+[a-zA-Z])-(\\d{1,2}\\.\\d{1,2}\\.\\d{4})");

String s = "a href=''SoFlash-12.22.2011/a";

Matcher m = p.matcher(s);

while (m.find()) {

// 通过调用group()方法里的索引 将url,标题和日期全部给打印出来

System.out.println("打印出url链接:" + m.group(1));

System.out.println("打印出标题:" + m.group(2));

System.out.println("打印出日期:" + m.group(3));

System.out.println();

}

System.out.println("group方法捕获的数据个数:" + m.groupCount() + "个");

}

}

下输出结果:

打印出url链接:

打印出标题:SoFlash

打印出日期:12.22.2011

group方法捕获的数据个数:3个

如何用java实现新闻采集

如果代码能力不是很好的话可以借助一些软件去抓取新闻的字段

把数据抓取下来以后可以选择存入数据库当中

需要去重的话,在Java的集合框架中就有HashSet和HashMap可以对数据去重

使用带Hash的集合时一定要注意,如果是存放自定义的对象一定要重写equals方法和HashCode方法

请教下如何使用java做实时网页数据采集?

String urlStr = ""; // 网址

try {

//创建一个url对象来指向要采集信息的网址

URL url = new URL(urlStr);

//将读取到的字节转化为字符

InputStreamReader inStrRead = new InputStreamReader(url.openStream(),"utf-8");

//读取InputStreamReader转化成的字符

BufferedReader bufRead = new BufferedReader(inStrRead);

//读到的内容不为空

while (bufRead.readLine() != null) {

System.out.println(bufRead.readLine());

}

bufRead.close();

} catch (IOException e) {

e.printStackTrace();

}

怎样用Java实现网站数据采集.txt

可以使用HttpClient读取网页的内容

整个过程分为六步

创建 HttpClient 的实例

2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址

3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例

4. 读 response

5. 释放连接。无论执行方法是否成功,都必须释放连接

6. 对得到后的内容进行处理

实现如下:

import java.io.IOException;

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientTest...{

public static void main(String[] args) {

//构造HttpClient的实例

HttpClient httpClient = new HttpClient();

//创建GET方法的实例

GetMethod getMethod = new GetMethod("");

//使用系统提供的默认的恢复策略

getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

new DefaultHttpMethodRetryHandler());

try {

//执行getMethod

int statusCode = httpClient.executeMethod(getMethod);

if (statusCode != HttpStatus.SC_OK) {

System.err.println("Method failed: "

+ getMethod.getStatusLine());

}

//读取内容

byte[] responseBody = getMethod.getResponseBoy();

//处理内容

System.out.println(new String(responseBody));

} catch (HttpException e) {

//发生异常,可能是协议不对或者返回的内容有问题

System.out.println("Please check your provided http address!");

e.printStackTrace();

} catch (IOException e) {

//发生网络异常

e.printStackTrace();

} finally {

//释放连接

getMethod.releaseConnection();

}

}

}

这样得到的是页面的源代码,再进行处理

java数据采集系统怎么样

java数据采集系统可以的。Java采集程序优点:可对采集过程实现精细控制。


网页题目:java代码采集 采集器代码
浏览地址:http://scyanting.com/article/hiphog.html