Java精確抽取網(wǎng)頁發(fā)布時(shí)間
對(duì)網(wǎng)頁中各種不同格式的發(fā)布時(shí)間進(jìn)行抽取,將發(fā)布時(shí)間以規(guī)整的“yyyy-MM-dd HH:mm:ss”格式表示出來,只能盡量追求精確,但是因?yàn)榫W(wǎng)絡(luò)發(fā)布時(shí)間的格式十分靈活,所以做不到百分百地正確抽取
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 獲取網(wǎng)頁的發(fā)布時(shí)間
*/
public class FetchPubTime {
/** 表示url中連續(xù)的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();
/** 格式正確的時(shí)間正則表達(dá)式*/
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里面抽取出發(fā)布時(shí)間,返回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());
//每一步都不能夠超出當(dāng)前時(shí)間
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;
}
/** 從網(wǎng)頁源碼中取出發(fā)布時(shí)間
* java中正則表達(dá)式提取字符串中日期實(shí)現(xiàn)代碼
* 2013年12月19日15:58:42
* 讀取出2013-12-19 15:48:33或者2013-12-19或者2012/3/05形式的時(shí)間
* @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);
//如果是僅僅抽取年月日,則按照上面的,如果是抽取年月日-時(shí)分秒,則按照下面的
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();
//取出第一個(gè)值
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;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringBoot+jsp項(xiàng)目啟動(dòng)出現(xiàn)404的解決方法
這篇文章主要介紹了SpringBoot+jsp項(xiàng)目啟動(dòng)出現(xiàn)404的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
Spring中@Conditional注解的詳細(xì)講解及示例
這篇文章主要介紹了Spring中@Conditional注解的詳細(xì)講解及示例,@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊(cè)bean,需要的朋友可以參考下2023-11-11
MyBatis利用MyCat實(shí)現(xiàn)多租戶的簡單思路分享
這篇文章主要給大家介紹了關(guān)于MyBatis利用MyCat實(shí)現(xiàn)多租戶的簡單思路的相關(guān)資料,文中的多租戶是基于多數(shù)據(jù)庫進(jìn)行實(shí)現(xiàn)的,數(shù)據(jù)是通過不同數(shù)據(jù)庫進(jìn)行隔離,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
java中JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解
這篇文章主要介紹了JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解,Java源代碼文件(.java后綴)會(huì)被Java編譯器編譯為字節(jié)碼文件,然后由JVM中的類加載器加載各個(gè)類的字節(jié)碼文件,加載完畢之后,交由JVM執(zhí)行引擎執(zhí)行。JVM中怎么存取數(shù)據(jù)和相關(guān)信息呢?,需要的朋友可以參考下2019-06-06
SpringBoot整合Swagger接口文檔工具的流程步驟
我們?cè)陂_發(fā)接口的時(shí)候,會(huì)將接口文檔給前端的開發(fā)者進(jìn)行對(duì)接,我們可以通過Postman或者Yapi等接口管理工具進(jìn)行編寫管理,實(shí)際開發(fā)中,接口的管理確實(shí)也應(yīng)該通過專業(yè)的工具管理,本文,我們就來談?wù)勗趺丛赟pringBoot整合Swagger接口文檔工具2023-08-08
java結(jié)合WebSphere MQ實(shí)現(xiàn)接收隊(duì)列文件功能
WebSphereMQ,也稱MQSeries,以一致的、可靠的和易于管理的方式來連接應(yīng)用程序,并為跨部門、企業(yè)范圍的集成提供了可靠的基礎(chǔ)。通過為重要的消息和事務(wù)提供可靠的、一次且僅一次的傳遞,MQ可以處理復(fù)雜的通信協(xié)議,并動(dòng)態(tài)地將消息傳遞工作負(fù)載分配給可用的資源。2015-10-10
Apache Dubbo的SPI機(jī)制是如何實(shí)現(xiàn)的
SPI全稱為Service Provider Interface,對(duì)應(yīng)中文為服務(wù)發(fā)現(xiàn)機(jī)制。SPI類似一種可插拔機(jī)制,首先需要定義一個(gè)接口或一個(gè)約定,然后不同的場景可以對(duì)其進(jìn)行實(shí)現(xiàn),調(diào)用方在使用的時(shí)候無需過多關(guān)注具體的實(shí)現(xiàn)細(xì)節(jié)。在Java中,SPI體現(xiàn)了面向接口編程的思想,滿足開閉設(shè)計(jì)原則。2021-06-06

