不用 html parser 的那个包 分析HTML我记得javax.swing.text.html.parser好想可以分析
HTML 的标签中的内容,具体怎么用忘记了望高手指点一二
HTML 的标签中的内容,具体怎么用忘记了望高手指点一二
解决方案 »
- 写的程序在tomcat上发布有问题
- 从数据库中读取图片地址和内容地址 并显示出来
- spring+hibernate 多文件如何保存到blob 在线等(急~)
- 问一下hibernate反转生成.hbm.xml的问题
- 文件上传问题!
- action 之间怎么传递form
- 连接字符串中的 characterEncoding 有什么作用?
- 请教JSP/SERVLET的编程高手!!!!!!!!!!!
- 谁可以提供一个jsp的留言板?
- 本人想做一个下载的demo,每次执行时页面会弹出对话框,“打开”,“保存”,“取消”。我想知道用户点的是哪个,好在后台处理记录信息。求帮助。
- ssh初学者的一个问题
- sun 的 httpservlet 包
获得 URL 连接
然后 调用什么DTD
复写这个javax.swing.text.html.parser
类 DocumentParser的方法就可以了
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class WebContent ...{
/** *//**
* 读取一个网页全部内容
*/
public String getOneHtml(String htmlurl) throws IOException...{
URL url;
String temp;
StringBuffer sb = new StringBuffer();
try ...{
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) ...{
sb.append(temp);
}
in.close();
}catch(MalformedURLException me)...{
System.out.println("你输入的URL格式有问题!请仔细输入");
me.getMessage();
throw me;
}catch (IOException e) ...{
e.printStackTrace();
throw e;
}
return sb.toString();
} /** *//**
*
* @param s
* @return 获得网页标题
*/
public String getTitle(String s) ...{
String regex;
String title = "";
List<String> list = new ArrayList<String>();
regex = "<title>.*?</title>";
Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
for (int i = 0; i < list.size(); i++) ...{
title = title + list.get(i);
}
return outTag(title);
} /** *//**
*
* @param s
* @return 获得链接
*/
public List<String> getLink(String s) ...{
String regex;
List<String> list = new ArrayList<String>();
regex = "<a[^>]*href=("([^"]*)"|'([^']*)'|([^\s>]*))[^>]*>(.*?)</a>";
Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
return list;
} /** *//**
*
* @param s
* @return 获得脚本代码
*/
public List<String> getScript(String s) ...{
String regex;
List<String> list = new ArrayList<String>();
regex = "<script.*?</script>";
Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
return list;
} /** *//**
*
* @param s
* @return 获得CSS
*/
public List<String> getCSS(String s) ...{
String regex;
List<String> list = new ArrayList<String>();
regex = "<style.*?</style>";
Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
return list;
} /** *//**
*
* @param s
* @return 去掉标记
*/
public String outTag(String s) ...{
return s.replaceAll("<.*?>", "");
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class WebContent ...{
/** *//**
* 读取一个网页全部内容
*/
public String getOneHtml(String htmlurl) throws IOException...{
URL url;
String temp;
StringBuffer sb = new StringBuffer();
try ...{
url = new URL(htmlurl);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream(), "utf-8"));// 读取网页全部内容
while ((temp = in.readLine()) != null) ...{
sb.append(temp);
}
in.close();
}catch(MalformedURLException me)...{
System.out.println("你输入的URL格式有问题!请仔细输入");
me.getMessage();
throw me;
}catch (IOException e) ...{
e.printStackTrace();
throw e;
}
return sb.toString();
} /** *//**
*
* @param s
* @return 获得网页标题
*/
public String getTitle(String s) ...{
String regex;
String title = "";
List<String> list = new ArrayList<String>();
regex = "<title>.*?</title>";
Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
for (int i = 0; i < list.size(); i++) ...{
title = title + list.get(i);
}
return outTag(title);
} /** *//**
*
* @param s
* @return 获得链接
*/
public List<String> getLink(String s) ...{
String regex;
List<String> list = new ArrayList<String>();
regex = "<a[^>]*href=("([^"]*)"|'([^']*)'|([^\s>]*))[^>]*>(.*?)</a>";
Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
return list;
} /** *//**
*
* @param s
* @return 获得脚本代码
*/
public List<String> getScript(String s) ...{
String regex;
List<String> list = new ArrayList<String>();
regex = "<script.*?</script>";
Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
return list;
} /** *//**
*
* @param s
* @return 获得CSS
*/
public List<String> getCSS(String s) ...{
String regex;
List<String> list = new ArrayList<String>();
regex = "<style.*?</style>";
Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(s);
while (ma.find()) ...{
list.add(ma.group());
}
return list;
} /** *//**
*
* @param s
* @return 去掉标记
*/
public String outTag(String s) ...{
return s.replaceAll("<.*?>", "");
}
比如说从某个网页中盗取下载链接,还是提取网页的摘要文字说明什么的。1.获取网页内容。
代码参照12楼的方法getOneHtml。2.按照关键字查询,字符串查询,这个就不用细说了吧。如果提取内容比较复杂,有各种情况的话,就比较复杂了,但是也可以做,
就是事先定义pattern文件,然后按照定义搜索。最复杂的是让你的程序彻底理解网页的内容,这个就比较难了,是浏览器的工作对不对。
不过也有对策,据说Java Mozilla Html Parser能满足要求,本人也没有用过,
请参照下面的网页,学会了别忘了把经验说与大家分享噢。http://www.open-open.com/open231130.htm