package html.regex;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;public class AstroExtractTest {
//输出文件位置
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 body = null;
//文件输出流
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new FileWriter(new File(sorePath)));
//获得网页文本内容
String str = AstroExtractTest
.getDocumentAt(url);
//创建提取标题和正文的正则表达式
Pattern pt_title = Pattern
.compile(
"<span>(.*)<em>(.*)</em></span>",
Pattern.MULTILINE | Pattern.DOTALL);

//创建作者的正则表达式
Pattern pt_author =Pattern.compile("<cite>(.*)</cite>");
//创建提取星座信息的正则表达式
Pattern pt_yun = Pattern.compile("<div><h4>(.*)</h4><p><img><img></p></div>");
Pattern pt_yun1 = Pattern.compile("<div><h4>(.*)</h4><p><img><img><img></p></div>");
Pattern pt_yun2 = Pattern.compile("<div><h4>(.*)</h4><p><img><img></p></div>");
Pattern pt_yun3 = Pattern.compile("<div><h4>(.*)</h4><p><img><img></p></div>");
//创建提取星座运程信息的正则表达式
Pattern pt_zhi = Pattern.compile("<div><h4>(.*)</h4><p>(.*)</p></div>");
Pattern pt_zhi1 = Pattern.compile("<div><h4>(.*)</h4><p>(.*)</p></div>");
Pattern pt_zhi2 = Pattern.compile("<div><h4>(.*)</h4><p>(.*)</p></div>");
Pattern pt_zhi3 = Pattern.compile("<div><h4>(.*)</h4><p>(.*)</p></div>");
Pattern pt_zhi4 = Pattern.compile("<div><h4>(.*)</h4><p>(.*)</p></div>");
//创建提取星座运程总结的正则表达式
Pattern pt_summary = Pattern.compile("<div>(.*)<br/>(.*)<br/></div>", Pattern.DOTALL);

Matcher mc = pt_title.matcher(str);
while (mc.find()) {
//提取标题
title = mc.group(1).trim();
//提取正文
body = mc.group(3);

//写入标题到输入文件
bw.write(title+"\r\n");
}
mc = pt_author.matcher(body);
while (mc.find()) {
//写入星座信息到输入文件
bw.write(mc.group(1).trim()+"\r\n");
}
//提取星座信息
mc = pt_yun.matcher(body);
while (mc.find()) {
//写入星座信息到输入文件
bw.write(mc.group(1).trim()+"\r\n");
}
mc = pt_yun1.matcher(body);
while (mc.find()) {
//写入星座信息到输入文件
bw.write(mc.group(1).trim()+"\r\n");
}
mc = pt_yun2.matcher(body);
while (mc.find()) {
//写入星座信息到输入文件
bw.write(mc.group(1).trim()+"\r\n");
}
mc = pt_yun3.matcher(body);
while (mc.find()) {
//写入星座信息到输入文件
bw.write(mc.group(1).trim()+"\r\n");
}
//提取运程信息
mc = pt_zhi.matcher(body);
while (mc.find()) {
int count=mc.groupCount();
//写入运程信息到输入文件
for(int i=1;i<=count;i++)
{
bw.write(mc.group(i).trim()+"\r\n");
}
}
mc = pt_zhi1.matcher(body);
while (mc.find()) {
int count=mc.groupCount();
//写入运程信息到输入文件
for(int i=1;i<=count;i++)
{
bw.write(mc.group(i).trim()+"\r\n");
}
}
mc = pt_zhi2.matcher(body);
while (mc.find()) {
int count=mc.groupCount();
//写入运程信息到输入文件
for(int i=1;i<=count;i++)
{
bw.write(mc.group(i).trim()+"\r\n");
}
}
mc = pt_zhi3.matcher(body);
while (mc.find()) {
int count=mc.groupCount();
//写入运程信息到输入文件
for(int i=1;i<=count;i++)
{
bw.write(mc.group(i).trim()+"\r\n");
}
}
mc = pt_zhi4.matcher(body);
while (mc.find()) {
int count=mc.groupCount();
//写入运程信息到输入文件
for(int i=1;i<=count;i++)
{
bw.write(mc.group(i).trim()+"\r\n");
}
}
//提取运程总结信息
mc=pt_summary.matcher(body);
while (mc.find()) {
//写入运程总结到输入文件
bw.write(mc.group(1).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) {
AstroExtractTest astroExtractor=new AstroExtractTest();
try{
astroExtractor.extractUrl("http://astro.sina.com.cn/fate/astro_aries.html?prourl=8");
}catch(IOException e){
e.printStackTrace();
}
}
}小弟刚刚学习htmlparser
按照书上改了改 ,可还是不能解析出来网页信息
有高手指点下,要是解析本地的HTML文件要怎么解析呢