使用正则表达式抽取新闻/BBS网页发表时间(修改版)

简介:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 分析时间戳
 * 
 * @author xum
 * 
 */
public class PublishTimeExtract {
 
private static final String TIME_REGEX = "((:|>|\\s)?20[0-9]{2}(-|/|\\.|\\u5e74)\\d{1,2}(-|/|\\.|\\u6708)\\d{1,2}(\\u65e5)?\\s?\\d{1,2}(:|\\u65f6)\\d{2}((:|\\u5206)\\d{2})?|(:|>|\\s)?[0-9]{2}\\u5e74\\d{1,2}\\u6708\\d{1,2}(\\u65e5)?\\s?\\d{1,2}(:|\\u65f6)\\d{2}((:|\\u5206)\\d{2})?)";
private static final String TIME_REGEX_1 = "((:|>|\\s)?20[0-9]{2}(-|/|\\.|\\u5e74)\\d{1,2}(-|/|\\.|\\u6708)\\d{1,2}(\\u65e5)?(\\s|&nbsp|<)+|(:|>|\\s)?[0-9]{2}\\u5e74\\d{1,2}\\u6708\\d{1,2}(\\u65e5)?(\\s|&nbsp|<)+)";
private static Pattern pattern = Pattern.compile(TIME_REGEX);
private static Pattern pattern1 = Pattern.compile(TIME_REGEX_1);
private static SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private static final String BBS_URL = "(http://bbs/\\..*|http://www\\.tianya\\.cn/[a-zA-Z]*forum/content/.*)";
 
/**
 * @param content
 * @param url
 * @return
 */
public static String extractDate(String content, String url) {
 
String con = content
.replaceAll("<!--.*?-->", "");
 
Matcher m = pattern.matcher(con);
Date now = new Date();
 
//
if (url.matches(BBS_URL)) {
 
String dateStr = null;
 
Date date = null;
 
while (m.find()) {
 
dateStr = m.group();
 
if (dateStr == null)
continue;
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
 
dateStr = dateStr.replaceAll("\\u65f6|\\u5206", ":");
 
Date tempDate;
 
try {
tempDate = sdf.parse(changeString2Date(dateStr));
 
if (tempDate.after(now)) {
continue;
}
 
} catch (ParseException e) {
continue;
}
 
if (date == null) {
date = tempDate;
} else if (tempDate.after(date)) {
date = tempDate;
}
}
 
if (date != null) {
 
return date.getTime() + "";
}
 
} else {
 
String dateStr = null;
 
if (m.find()) {
dateStr = m.group();
}
 
if (dateStr != null) {
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
;
dateStr = dateStr.replaceAll("\\u65f6|\\u5206", ":");
 
try {
 
return (sdf.parse(changeString2Date(dateStr)).getTime())
+ "";
 
} catch (ParseException e) {
System.out
.println("=======================>parsedate error: "
+ e.getMessage());
return (new Date()).getTime() + "";
}
}
}
 
m = pattern1.matcher(con);
 
if (url.matches(BBS_URL)) {
 
String dateStr = null;
 
Date date = null;
 
while (m.find()) {
 
dateStr = m.group();
 
if (dateStr == null)
continue;
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
 
dateStr = dateStr.replaceAll("(&nbsp|<)", "");
 
Date tempDate;
 
try {
tempDate = sdf.parse(changeString2Date(dateStr));
 
if (tempDate.after(now)) {
continue;
}
 
} catch (ParseException e) {
continue;
}
 
if (date == null) {
date = tempDate;
} else if (tempDate.after(date)) {
date = tempDate;
}
}
 
if (date != null) {
 
return date.getTime() + "";
}
 
} else {
 
String dateStr = null;
 
if (m.find()) {
dateStr = m.group();
}
 
if (dateStr != null) {
 
dateStr = dateStr.trim().replaceAll(">", "");
 
if (dateStr.startsWith(":")) {
dateStr = dateStr.replaceFirst(":", "");
}
 
dateStr = dateStr.replaceAll("\\.|/|\\u5e74|\\u6708", "-")
.replaceAll("\\u65e5", " ");
;
dateStr = dateStr.replaceAll("(&nbsp|<)", "");
 
try {
 
return (sdf.parse(changeString2Date(dateStr)).getTime())
+ "";
 
} catch (ParseException e) {
System.out
.println("=======================>parsedate error: "
+ e.getMessage());
return (new Date()).getTime() + "";
}
}
}
 
return (new Date()).getTime() + "";
}
 
private static String changeString2Date(String dateStr) {
 
System.out.println("=======================>Pre Date: " + dateStr);
 
String date = "";
 
String[] tempDateStr = dateStr.trim().replaceAll("\\s+", "@")
.replaceAll("(-|:)", "@").split("@");
 
if (tempDateStr.length > 0) {
 
String year = tempDateStr[0];
 
if (year.length() == 2) {
year = "20" + year;
}
 
date = date + year + "-";
}
 
if (tempDateStr.length > 1) {
date = date
+ (tempDateStr[1].length() > 1 ? tempDateStr[1] : "0"
+ tempDateStr[1]) + "-";
}
 
if (tempDateStr.length > 2) {
date = date
+ (tempDateStr[2].length() > 1 ? tempDateStr[2] : "0"
+ tempDateStr[2]) + " ";
}
 
if (tempDateStr.length > 3) {
date = date
+ (tempDateStr[3].length() > 1 ? tempDateStr[3] : "0"
+ tempDateStr[3]) + ":";
} else {
date = date + "00:";
}
 
if (tempDateStr.length > 4) {
date = date
+ (tempDateStr[4].length() > 1 ? tempDateStr[4] : "0"
+ tempDateStr[4]) + ":";
} else {
date = date + "00:";
}
 
if (tempDateStr.length > 5) {
date = date
+ (tempDateStr[5].length() > 1 ? tempDateStr[5] : "0"
+ tempDateStr[5]);
} else {
date = date + "00";
}
 
System.out.println("=======================> Date: " + date);
 
return date;
}
 
public static void main(String[] args) {
 
extractDate(
" <td align=\"center\">http://www.rednet.cn &nbsp;<!--时间开始-->2012/3/2 9:14:36<!--时间结束-->&nbsp;&nbsp;",
"");
 
}

本文转自william_xu 51CTO博客,原文链接:http://blog.51cto.com/williamx/806457,如需转载请自行联系原作者
相关文章
|
3月前
|
数据采集 Web App开发 iOS开发
如何使用 Python 语言的正则表达式进行网页数据的爬取?
使用 Python 进行网页数据爬取的步骤包括:1. 安装必要库(requests、re、bs4);2. 发送 HTTP 请求获取网页内容;3. 使用正则表达式提取数据;4. 数据清洗和处理;5. 循环遍历多个页面。通过这些步骤,可以高效地从网页中提取所需信息。
|
6月前
|
Python
"揭秘!Python如何运用神秘的正则表达式,轻松穿梭于网页迷宫,一键抓取隐藏链接?"
【8月更文挑战第21天】Python凭借其强大的编程能力,在数据抓取和网页解析领域表现出高效与灵活。通过结合requests库进行网页请求及正则表达式进行复杂文本模式匹配,可轻松提取网页信息。本示例展示如何使用Python和正则表达式解析网页链接。首先确保已安装requests库,可通过`pip install requests`安装。接着,利用requests获取网页内容,并使用正则表达式提取所有`&lt;a&gt;`标签的`href`属性。
69 0
|
9月前
|
Python
使用Python解析网页和正则表达式
使用Python解析网页涉及`requests`和`re`模块。首先导入这两个模块,然后用`requests.get()`发送HTTP请求获取URL内容。通过`.text`属性得到HTML文本。接着,利用正则表达式和`re.search()`匹配特定模式(如网页标题),并用`.group(1)`获取匹配数据。最后,对提取的信息进行处理,如打印标题。实际操作时,需根据需求调整正则表达式。
98 2
|
9月前
|
Python
如何使用正则表达式提取网页中的特定信息
如何使用正则表达式提取网页中的特定信息
262 1
|
Python
python解析网页,正则表达式
python解析网页,正则表达式
91 0
|
Python
使用Python解析网页和正则表达式
要使用Python解析网页和正则表达式,您可以使用以下步骤:
102 1
|
Java 移动开发
java正则表达式移除网页中注释代码
/** * 移除网页中注释掉的代码 * * @param str * @return */ public static String removedisablecode(String str) { Pattern pattern = Pattern.
1257 0
|
Java
java 使用正则表达式从网页上提取网站标题
如何从网页上抓取有价值的东西?看懂了下面的程序(非常简单),想从网页上抓取什么信息(标题、内容、Email、价格等)就能抓取什么信息。 package catchhtml; import java.
1450 0
|
8月前
|
Python
Python正则表达式详解:掌握文本匹配的魔法
Python正则表达式详解:掌握文本匹配的魔法