做项目时,并没有合作公司的获取新闻的接口,但是项目又急着上线,所以总监就让我来做一个简单的抓取,现将主要的工具类NewsUtil.java贴出来供大家参考。

NewsUtil.java

 package org.news.util;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* 抓取新闻内容的辅助类
* @author geenkDC
* @time 2015-07-28 15:15:04
*/
public class NewsUtil {
/**
* 通过提交的URL来抓取出新闻的链接
* @param url
* @return
* @throws Exception
*/
public static ArrayList<String> findUrlByUrl(String url) throws Exception
{
URL url0=new URL(url);
ArrayList<String> urlList=new ArrayList<String>();
URLConnection con;
BufferedReader br=null;
try {
con = url0.openConnection();
InputStream in=con.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
String str="";
while((str=br.readLine())!=null)
{
urlList.addAll(findUrl(str));
}
} catch (IOException e) {
throw new RuntimeException("URL读写错误:"+e.getMessage());
}
if(br!=null)
{
try {
br.close();
} catch (IOException e) {
throw new RuntimeException("URL流关闭异常:"+e.getMessage());
}
}
return urlList;
} /**抓取新闻URL的真正实现类
* @param str
* @return
*/
public static ArrayList<String> findUrl(String str)
{
ArrayList<String> urlList=new ArrayList<String>();
//匹配新闻的URL
String regex="http://[a-zA-Z0-9_\\.:\\d/?=&%]+\\.jhtml";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(str);
//找符合正则匹配的字串
while(m.find())
{
String subStr=m.group().substring(m.group().lastIndexOf("/")+1, m.group().lastIndexOf(".jhtml")); try {
if (subStr.matches("[0-9]*")) {
urlList.add(m.group()); }
} catch (Exception e) {
throw new RuntimeException("匹配新闻URL出错:"+e.getMessage());
}
}
return urlList;
} /**
* 根据URL找到其的新闻内容
* @param url
* @return
* @throws Exception
*/
public static ArrayList<String> findContentByUrl(String url) throws Exception {
URL url1=new URL(url);
ArrayList<String> conList=new ArrayList<String>();
URLConnection con;
BufferedReader br=null;
try {
con = url1.openConnection();
InputStream in=con.getInputStream();
InputStreamReader isr=new InputStreamReader(in, "utf-8");
br=new BufferedReader(isr);
String str="";
StringBuffer sb=new StringBuffer();
while((str=br.readLine())!=null)
{
sb.append(str);
}
conList.addAll(findContent(sb.toString()));
} catch (IOException e) {
throw new RuntimeException("URL读写错误:"+e.getMessage());
}
if(br!=null)
{
try {
br.close();
} catch (IOException e) {
throw new RuntimeException("URL流关闭异常:"+e.getMessage());
}
}
return conList;
} /**
* 抓取新闻内容的真正实现类
* @param str
* @return
*/
public static ArrayList<String> findContent(String str) {
ArrayList<String> strList=new ArrayList<String>();
//匹配新闻内容div
String regex="<div class=\"con_box\">([\\s\\S]*)</div>([\\s\\S]*)<div class=\"left_con\">";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(str);
//找符合正则匹配的字串
while(m.find())
{
try {
strList.add(new String(m.group()));
} catch (Exception e) {
throw new RuntimeException("抓取新闻内容出错:"+e.getMessage());
}
}
return strList;
}
}

功能简单说明:

  只要输入网站首页的url,程序会自动获取匹配的新闻条目的url,再根据每个新闻条目的url抓取该新闻的左右内容。

java实现抓取某公司官网新闻的更多相关文章

  1. iOS—网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据

    网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...

  2. iOS开发——网络实用技术OC篇&网络爬虫-使用java语言抓取网络数据

    网络爬虫-使用java语言抓取网络数据 前提:熟悉java语法(能看懂就行) 准备阶段:从网页中获取html代码 实战阶段:将对应的html代码使用java语言解析出来,最后保存到plist文件 上一 ...

  3. 如何使用JAVA语言抓取某个网页中的邮箱地址

    现实生活中咱们常常在浏览网页时看到自己需要的信息,但由于信息过于庞大而又不能逐个保存下来. 接下来,咱们就以获取邮箱地址为例,使用java语言抓取网页中的邮箱地址 实现思路如下: 1.使用Java.n ...

  4. 如何从sun公司官网下载java API文档(转载)

    相信很多同人和我一样,想去官网下载一份纯英文的java API文档,可使sun公司的网站让我实在很头疼,很乱,全是英文!所以就在网上下载了别人提供的下载!可是还是不甘心!其实多去看看这些英文的技术网站 ...

  5. java网页抓取

    网页抓取就是,我们想要从别人的网站上得到我们想要的,也算是窃取了,有的网站就对这个网页抓取就做了限制,比如百度 直接进入正题 //要抓取的网页地址 String urlStr = "http ...

  6. Java数据抓取经验【转载】

    本人担任职友集的java工程师五年,其中抓取数据占主要的一部分,抓取的信息只要有两部分,职位和简历,其中职位的抓取量为日均插入量为30万,更新量 为60万,抓取全国300多个人才网站.职友集(现在改名 ...

  7. 【java】抓取页面内容,提取链接(此方法可以http get无需账号密码的请求)

    package 网络编程; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutpu ...

  8. java爬虫抓取腾讯漫画评论

    package com.eteclab.wodm.utils; import java.io.BufferedWriter; import java.io.File; import java.io.F ...

  9. 网络爬虫Java实现抓取网页内容

    package 抓取网页; import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream; ...

随机推荐

  1. codeforces 333B - Chips

    注意:横向纵向交叉时,只要两条边不是正中的边(当n&1!=1),就可以余下两个chip. 代码里数组a[][]第二维下标 0表示横向边,1表示纵向边. #include<stdio.h& ...

  2. qq互联(connect.qq.com)取用户信息的方法

    <?php //应用的APPID$app_id = "YOUR_APP_ID";//应用的APPKEY$app_secret = "YOUR_APP_KEY&quo ...

  3. OOP——UML六种关系

    UML定义的关系主要有:泛化.实现.依赖.关联.聚合.组合,这六种关系紧密程度依次加强,分别看一下 泛化 概念:泛化是一种一般与特殊.一般与具体之间关系的描述,具体描述建立在一般描述的基础之上,并对其 ...

  4. 如何获取数据块结构信息dump

    有个pub_department的表,索引为PK_PUB_DEPARTMENT. 1.找到object_id select   object_id from dba_objects s  where  ...

  5. hdu 1175(广搜)

    题意:容易理解... 思路:我开始的思路不好实现,而且有漏洞,时间复杂度也高,后来在网上学了下别人的方法,真心感觉很牛B,不仅代码好实现,而且时间复杂度比较低,具体看代码实现吧!! 代码实现: #in ...

  6. IOS AVAUDIOPLAYER 播放器使用

    1. 导入 AVFoundation.framework 2.导入头文件  #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...

  7. DOM笔记(三):Element接口和HTMLElement接口

    一.Element接口 Element接口表示一个元素,该接口扩展自Node接口,自然继承了Node接口的属性和方法,也有一套针对元素的属性和方法. Element接口常见的属性比较少,常用的就是一个 ...

  8. bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...

  9. iOS Development Learning 13Nov

    关注了关东升老师在博客园的iOS开发博客. 在使用能力课堂观看智捷课堂的iOS8开发视频教程.观看到Part1 课时3 Xcode中的iOS工程模板

  10. Tasks Queues and Cron Jobs(任务队列和时钟守护作业)

    一个网络应用必须迅速响应网络请求,一般要小于1s,最好是几十微秒,以便为坐在浏览器前面的用户提供一个流畅的体验.这就给不了应用太多的时间来处理工作.有时会是有更多的工作要做而不是有时间来做它.< ...