正则表达式提取tom星座内容

正则表达式提取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();
}
}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值