正则表达式提取tom网页内容。http://astro.tom.com/sagittarius.html
写道
package rex.simple;
import java.io.*;
import java.net.*;
import java.util.regex.*;
public class AstroExtractor {
//输出文件位置
private String sorePath="c:\\astro.txt";
//从一个URL下载HTML文本到一个本地字符串
private static String getDocumentAt(String urlString) {
//本地字符串缓冲区
StringBuffer html_text = new StringBuffer();
try {
//根据urlString创建一个指向Web的网络资源
URL url = new URL(urlString);
//创建一个到该网络资源的连接
URLConnection conn = url.openConnection();
//创建输入流
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
//逐行读取文本放到缓冲区
while ((line = reader.readLine()) != null)
html_text.append(line + "\n");
reader.close();
} catch (MalformedURLException e) {
System.out.println("无效的URL: " + urlString);
} catch (IOException e) {
e.printStackTrace();
}
return html_text.toString();
}
//从一个字符串提取信息
public void extractUrl(String url)throws IOException{
//标题
String title = null;
//日期
String days = null;
//详细信息
String detail = null;
//正文
String body = null;
//文件输出流
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new FileWriter(new File(sorePath)));
//获得网页文本内容
String str = AstroExtractor
.getDocumentAt(url);
//创建提取标题和正文的正则表达式
Pattern pt_title = Pattern
.compile(
"<div class=\"astro_title\"><strong>(.*)</strong>(.*)</div>$(.*)<strong>★(.*)<!-- mcol内容区域 结束 -->",
Pattern.MULTILINE | Pattern.DOTALL);
//创建提取星座信息的正则表达式
Pattern pt_constellation = Pattern.compile("<strong>(.*)</strong>(.*)<br />(.*)<strong>(.*)</strong>(.*)<br />(.*)<strong>(.*)</strong>(.*)<br />(.*)" +
"<strong>(.*)</strong>(.*)<br />(.*)<strong>(.*)</strong>(.*)<br />(.*)<strong>(.*)</strong>(.*)<br />",
Pattern. MULTILINE| Pattern.DOTALL);
//创建提取星座运程总结的正则表达式
Pattern pt_summary = Pattern.compile("<br />(.*)<br />(.*)<br />", Pattern.DOTALL);
Matcher mc = pt_title.matcher(str);
while (mc.find()) {
// 提取标题
title = mc.group(1).trim();
// 提取日期
days = mc.group(2).trim();
// 提取详细信息
detail = mc.group(3).trim();
// 提取正文
body = mc.group(4);
//写入标题到输入文件
bw.write(title+"\r\n");
//写入日期到输入文件
bw.write(days+"\r\n");
}
//提取星座基本信息
mc = pt_constellation.matcher(detail);
while (mc.find()) {
int count=mc.groupCount();
for(int i=1;i<= count;i++)
{
int pos = i % 3 ;
String tt = mc.group(i).trim();
//写入星座信息到输入文件
if(pos == 1) {
bw.write( tt );
} else if(pos == 2) {
bw.write( tt +"\r\n");
}
}
}
//提取运程总结信息
mc=pt_summary.matcher(body);
while (mc.find()) {
//写入运程总结到输入文件
bw.write(mc.group(1).trim()+"\r\n");
bw.write(mc.group(2).trim()+"\r\n");
}
}catch(IOException e){
e.printStackTrace();
}catch(PatternSyntaxException e){
System.out.println("正则表达式语法错误");
}catch(IllegalStateException e){
System.out.println("找不到匹配字符串");
}
finally{
if(bw!=null)
bw.close();
}
}
public static void main(String[] args) {
AstroExtractor astroExtractor=new AstroExtractor();
try{
astroExtractor.extractUrl("http://astro.tom.com/sagittarius.html");
//astroExtractor.extractUrl("http://astro.sina.com.cn/sagittarius.html");
}catch(IOException e){
e.printStackTrace();
}
}
}
分享到:
相关推荐
主要介绍了Java使用正则表达式提取XML节点内容的方法,结合具体实例形式分析了java针对xml格式字符串的正则匹配相关操作技巧,需要的朋友可以参考下
正则表达式提取图片,通过正则表达式提取段落中的问题,便于排版和美观
而本软件正是把强大的正则功能完美地添加到表格中,让 Excel、WPS 支持正则表达式的搜索、匹配提取、替换、定位等,让数据处理能力进化一个层次! 让 Office Excel、WPS 表格支持正则表达式的免费插件:「Excel ...
Jmeter-使用正则表达式提取器获取关联参数的使用方法,网络资源共享!
正则表达式,正则表达式,正则表达式 正则表达式 正则表达式 正则表达式 正则表达式 正则表达式 正则表达式
excel自定义函数,在指定文本中使用正则表达式提取其中的符合文本并以指定分隔符分隔,如:文本:a1b2c3d4 正则:\d 分隔符:| 结果:1|2|3|4
java实现用正则表达式的方法提取html中的信息,可以提取标题,正文,链接等。经过运行,没问题的
如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么...本文接下来的内容先简要地介绍正则表达式的入门知识,然后以Jakarta-ORO API为例介绍如何使用正则表达式。
正则表达式汇总
本文实例讲述了python使用正则表达式提取网页URL的方法。分享给大家供大家参考。具体实现方法如下: import re import urllib url=//www.jb51.net s=urllib.urlopen(url).read() ss=s.replace( ,) urls=re.findall...
NULL 博文链接:https://duanhengbin.iteye.com/blog/1962431
常用正则表达式 正则表达式 常用正则表达式 正则表达式
一个用于正则匹配检查的工具,用于使用正则表达式提取字符串!
C#语言实现采用正则表达式提取网页上需要的数据,并将数据存储到数据库sql sever 2005
正则表达式.rar正则表达式.rar正则表达式.rar正则表达式.rar
《学习正则表达式》从正则表达式的基本概念讲起,到编写完整的sed和Perl脚本,再到转换HTML文件,将这种强大的工具解释得清晰透彻。...《学习正则表达式》适合对正则表达式感兴趣的程序员和互联网从业者。
正则表达式验证工具 V1.0 本软件主要用于检测正则表达式是否正确。 运行环境:本软件为绿色软件,无需安装,但需要Microsoft .NET Framework 4 支持,如果没有请前去下载(下载路径:...
正则表达式日期校验 正则表达式日期校验 正则表达式日期校验
正则表达式正则表达式正则表达式正则表达式 验证程序
正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式正则表达式...