Java精确抽取网页发布时间
作者:lijiao 发布时间:2022-03-24 17:20:11
标签:Java,抽取,时间
对网页中各种不同格式的发布时间进行抽取,将发布时间以规整的“yyyy-MM-dd HH:mm:ss”格式表示出来,只能尽量追求精确,但是因为网络发布时间的格式十分灵活,所以做不到百分百地正确抽取
package whu.extract.pubtime.core;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import whu.utils.TimeUtil;
/**
* Created On 2014年3月13日 下午2:49:05
* @description 获取网页的发布时间
*/
public class FetchPubTime {
/** 表示url中连续的8位日期,例如http://www.baidu.com/20140311/2356.html */
private static String url_reg_whole= "([-|/|_]{1}20\\d{6})";
/** 表示 用-或者/隔开的日期,有年月日的,例如 http://www.baidu.com/2014-3-11/2356.html */
private static String url_reg_sep_ymd = "([-|/|_]{1}20\\d{2}[-|/|_]{1}\\d{1,2}[-|/|_]{1}\\d{1,2})";
/** 表示 用-或者/隔开的日期,只有年和月份的,例如 http://www.baidu.com/2014-3/2356.html */
private static String url_reg_sep_ym = "([-|/|_]{1}20\\d{2}[-|/|_]{1}\\d{1,2})";
private static Calendar current = Calendar.getInstance();
/** 格式正确的时间正则表达式*/
private static String rightTimeReg = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
/**
* @param url
* @param urlContent
* @return
*/
public static String getPubTimeVarious(String url,String urlContent) {
String pubTime = getPubTimeFromUrl(url);
//链接里面没有,匹配文本中的
if(pubTime == null)
{
if(urlContent!=null&&!urlContent.trim().equals(""))
return extractPageDate(urlContent);
}
return pubTime;
}
/**从url里面抽取出发布时间,返回YYYY-MM-DD HH:mm:ss格式的字符串
* @param url
* @return
*/
public static String getPubTimeFromUrl(String url)
{
Pattern p_whole = Pattern.compile(url_reg_whole);
Matcher m_whole = p_whole.matcher(url);
if(m_whole.find(0)&&m_whole.groupCount()>0)
{
String time = m_whole.group(0);
time = time.substring(1,time.length());
//每一步都不能够超出当前时间
if(current.compareTo(TimeUtil.strToCalendar(time, "yyyyMMdd"))>=0)
{
return time.substring(0,4)+"-"+time.substring(4,6)+"-"+
time.substring(6,8)+" "+"00:00:00";
}
}
p_whole = null;
m_whole = null;
Pattern p_sep = Pattern.compile(url_reg_sep_ymd);
Matcher m_sep = p_sep.matcher(url);
if(m_sep.find(0)&&m_sep.groupCount()>0)
{
String time = m_sep.group(0);
time = time.substring(1,time.length());
String[] seg = time.split("[-|/|_]{1}");
Calendar theTime = Calendar.getInstance();
theTime.set(Calendar.YEAR,Integer.parseInt(seg[0]));
theTime.set(Calendar.MONTH, Integer.parseInt(seg[1]));
theTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(seg[2]));
if(current.compareTo(theTime)>=0)
{
return seg[0]+"-"+seg[1]+"-"+seg[2]+" "+"00:00:00";
}
}
p_sep = null;
m_sep = null;
Pattern p_sep_ym = Pattern.compile(url_reg_sep_ym);
Matcher m_sep_ym = p_sep_ym.matcher(url);
if(m_sep_ym.find(0)&&m_sep_ym.groupCount()>0)
{
String time = m_sep_ym.group(0);
time = time.substring(1,time.length());
Calendar theTime = Calendar.getInstance();
String[] seg = time.split("[-|/|_]{1}");
theTime.set(Calendar.YEAR,Integer.parseInt(seg[0]));
theTime.set(Calendar.MONTH, Integer.parseInt(seg[1]));
theTime.set(Calendar.DAY_OF_MONTH, 1);
if(current.compareTo(theTime)>=0)
{
return seg[0]+"-"+seg[1]+"-"+"01"+" "+"00:00:00";
}
}
return null;
}
/** 从网页源码中取出发布时间
* java中正则表达式提取字符串中日期实现代码
* 2013年12月19日15:58:42
* 读取出2013-12-19 15:48:33或者2013-12-19或者2012/3/05形式的时间
* @param text 待提取的字符串
* @return 返回日期
* @author: oschina
* @Createtime: Jan 21, 2013
*/
public static String extractPageDate(String text) {
boolean containsHMS =false;
String dateStr = text.replaceAll("r?n", " ");
try {
List matches = null;
Pattern p_detail = Pattern.compile("(20\\d{2}[-/]\\d{1,2}[-/]\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2})|(20\\d{2}年\\d{1,2}月\\d{1,2}日)", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
//如果是仅仅抽取年月日,则按照上面的,如果是抽取年月日-时分秒,则按照下面的
Pattern p = Pattern.compile("(20\\d{2}[-/]\\d{1,2}[-/]\\d{1,2})|(20\\d{2}年\\d{1,2}月\\d{1,2}日)", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
//Matcher matcher = p.matcher(dateStr);
Matcher matcher_detail = p_detail.matcher(dateStr);
if(!(matcher_detail.find(0) && matcher_detail.groupCount() >= 1))
{
matcher_detail = p.matcher(dateStr);
containsHMS = true;
}else
matcher_detail = p_detail.matcher(dateStr);
if (matcher_detail.find() && matcher_detail.groupCount() >= 1) {
matches = new ArrayList();
for (int i = 1; i <= matcher_detail.groupCount(); i++) {
String temp = matcher_detail.group(i);
matches.add(temp);
}
} else {
matches = Collections.EMPTY_LIST;
}
if (matches.size() > 0) {
for(int i=0;i<matches.size();i++)
{
String pubTime = matches.get(i).toString().trim();
//取出第一个值
pubTime = pubTime.replace("/", "-").replace("年", "-").replace("月", "-").replace("日", "-");
if(current.compareTo(TimeUtil.strToCalendar(pubTime, "yyyy-MM-dd"))>=0)
{
if(containsHMS)
pubTime+=" "+"00:00:00";
if(pubTime.matches(rightTimeReg))
{
return pubTime;
}
}
}
} else {
return null;
}
} catch (Exception e) {
return null;
}
return null;
}
}


猜你喜欢
- 一、环境准备:(根据自己电脑配置来选择安装版本,我的电脑是64位,所以此处选择64位安装)JDK下载:JDK 1.8下载地址: http:/
- Swagger是一款遵循 Restful 风格的接口文档开发神器,支持基于 API 自动生成接口文档,接口文档始终与 API 保持同步,不再
- SpringMVC * 介绍springMVC 中的 * 用于拦截控制器方法的执行。先创建出前置需要的一些条件:<a th:href=
- 概述本文的编写初衷,是想了解一下Spring Boot2中,具体是怎么序列化和反序列化JSR 310日期时间体系的,Spring MVC应用
- Map集合的概述概述:interface Map<K,V> 其中K是键的类型,键是唯一的,不重复。V是值的类型,是可以重复。且每
- 先来看看效果:一、添加依赖库的步骤1.项目的gradle文件内的做以下改动allprojects { repositories
- MyBatis缓存我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO
- 一、CAS(Compare And Set)Compare And Set(或Compare And Swap),CAS是解决多线程并行情况
- Chart控件可以用来绘制波形图、柱状图、饼图、折线图等,用来进行数据表现是很不错的,现在简单说一下这个控件的使用方法XAML:<Wi
- 栈和队列的本质是相同的,都只能在线性表的一端进行插入和删除。因此,栈和队列可以相互转换。用栈实现队列—力扣232题题目要求:仅使用两个栈实现
- 目录 1.ReentrantLock可重入锁概述2.可重入3.可打断4.锁超时5.公平锁6.条件变量 Condition1.Reentran
- 【1】首先我们定义一段假数据,这里以一个string为例字static void Main(string[] args){string da
- OkHttp(GitHub:https://github.com/square/okhttp) 的 Interceptor 就如同名称「拦截
- 昨天遇到了点问题解决浪费了一些时间(导致更新内容较少)回顾下问题项目出现Unable to import maven project: Se
- Flutter页面在软键盘弹出的时候,可以设置 Scaffold 的 resizeToAvoidBottomInset 属性来设置
- 寻找到application.yml的读取的操作。从spring.factories 中查看到# Application Listeners
- pom文件如果你的springboot项目要用到druid,那么这三个依赖必不可少:<dependency> &n
- Java方法重写(Override)与重载(Overload)的区别(超详细)首页在我们要学习这个知识点之前,应该要先了解什么是多态?在最初
- 大家真正在工作中开发 java 应用都会使用eclipse,myeclipse, IntelliJ等等不过依然值得花10分钟学习如何使用最原
- 目录或库文件名中包含汉字或空格的话,请将其用半角双引号括住。项目、属性、C/C++、附加包含目录:填写附加头文件所在目录 分号间隔多项项目、