高分求解JAVA中解析HTML的正则表达式写法

snowbirdfly 2010-10-26 04:25:27
最近获取网页页面,获取该网页页面的HTML源码,想获取其中的部分数据,HTML格式如下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
...(中间部分省略)
<!-- {start:list -->
<div class="grid-view">
<ul class="cls" id="data-table">
<li>
<div class="img"><a target="_blank" href="http://test.com/abcdefg"> <img src="http://img.mall.test.com/abcd.jpg"> </a></div>
<div class="title">
<a class="ico-b2c" href="http://co.test.com/content/help/2009-08-06/113746233677.html#1" target="_blank" title="测试数据1!"></a><h3><a target="_blank" href="http://book.test.com/">沸腾15年</a></h3>
<a class="xb-icon-small-1 no_name"></a> <a class="xb-icon-small-2 no_name"></a>
</div>
<div class="bot">
价格:<strong>75.<span class="small">00</span></strong>  
<a href="#" class="btn-im-online2" account="北京图书出版室"></a><br>
</div>
</li>
<li>
<div class="img"><a target="_blank" href="http://test.com/abcdefg2"> <img src="http://img.mall.test.com/abcd2.jpg"> </a></div>
<div class="title">
<a class="ico-b2c" href="http://co.test.com/content/help/2009-08-06/113746233677.html#1" target="_blank" title="测试数据2!"></a><h3><a target="_blank" href="http://book.test.com/">三国演义</a></h3>
<a class="xb-icon-small-1 no_name"></a> <a class="xb-icon-small-2 no_name"></a>
</div>
<div class="bot">
价格:<strong>255.<span class="small">00</span></strong>  
<a href="#" class="btn-im-online2" account="北京图书出版室"></a><br>
</div>
</li>
...(类似的数据)
</ul>
</div>
<!-- }end:list -->
...(后面省略的数据)

1、是想通过过滤,通过<div class="grid-view">
然后把这段数据首先获取下来,由于"grid-view"带有双引号,这个unicode字符无法通过正则表达式区分,查了下
http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/util/regex/Pattern.html
这个链接页面,针对Unicode 块和类别的类,\p{InGreek} Greek 块(简单块)中的字符。
测试,还是无法获取这段数据。

2、想获取每个<li>...<li>
中的
<div class="img"><a target="_blank" href="http://test.com/abcdefg"> <img src="http://img.mall.test.com/abcd.jpg">
获取这个img标记的图片的href以及图片的src,http://test.com/abcdefg以及http://img.mall.test.com/abcd.jpg。
以及产品名称:沸腾15年
产品价格:75

以前没怎么用过正则表达式,搜索看了很多资料,始终未找到和此应用相关的例子,请求高手解答下!
谢谢!!!
...全文
525 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
masterz 2010-10-27
  • 打赏
  • 举报
回复
解析html最好用parser,证则表达式效率比较低,自己写也比较困难。
http://htmlparser.sourceforge.net/
snowbirdfly 2010-10-27
  • 打赏
  • 举报
回复
不过产品名称和价格,我模仿前面的写法,但是还是不熟悉,获取不到
Matcher m = Pattern.compile("<div class=\"img\">\\s*<a[^<>]*href=([^<>]*)/?>\\s*<img\\s*src=([^<>]*)/?>\\s*<div class=\"title\">\\s*<a[^<>]* href=([^<>]*)\\s*>/?",Pattern.DOTALL).matcher(s);
哪位大侠指点下!
snowbirdfly 2010-10-27
  • 打赏
  • 举报
回复
感谢10楼的回复,测试通过,嘿嘿
Star_Ji 2010-10-27
  • 打赏
  • 举报
回复
用JQuery应该比较容易!
snowbirdfly 2010-10-27
  • 打赏
  • 举报
回复
嗯,谢谢8楼的回复,我这边有个同事也是用的这个,我之前以为通过正则表达式速度会快些,嘿嘿!
closewbq 2010-10-27
  • 打赏
  • 举报
回复

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("c:\\Test.txt"));
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = br.readLine()) != null) {
sb.append(str);
}
String[] arr = sb.toString().split("(?=<div class=\"grid-view\">)");
for (String s : arr) {
Matcher m =
Pattern
.compile(
"<div class=\"img\">\\s*<a[^<>]*href=([^<>]*)/?>\\s*<img\\s*src=([^<>]*)/?>",Pattern.DOTALL)
.matcher(s);
while(m.find()){
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
}

测试数据:c:Test.txt

<div class="grid-view">
<ul class="cls" id="data-table">
<li>
<div class="img"><a target="_blank" href="http://test.com/abcdefg"> <img src="http://img.mall.test.com/abcd.jpg"> </a></div>
<div class="title">
<a class="ico-b2c" href="http://co.test.com/content/help/2009-08-06/113746233677.html#1" target="_blank" title="??数据1!"></a><h3><a target="_blank" href="http://book.test.com/">11111111</a></h3>
<a class="xb-icon-small-1 no_name"></a> <a class="xb-icon-small-2 no_name"></a>
</div>
<div class="bot">
价格:<strong>75.<span class="small">00</span></strong>  
<a href="#" class="btn-im-online2" account="北京??出版室"></a><br>
</div>
</li>
<li>
<div class="img"><a target="_blank" href="http://test.com/abcdefg2"> <img src="http://img.mall.test.com/abcd2.jpg"> </a></div>
<div class="title">
<a class="ico-b2c" href="http://co.test.com/content/help/2009-08-06/113746233677.html#1" target="_blank" title="??数据2!"></a><h3><a target="_blank" href="http://book.test.com/">12312312312</a></h3>
<a class="xb-icon-small-1 no_name"></a> <a class="xb-icon-small-2 no_name"></a>
</div>
<div class="bot">
价格:<strong>255.<span class="small">00</span></strong>  
<a href="#" class="btn-im-online2" account="北京??出版室"></a><br>
</div>
</li>
...(?似的数据)
</ul>
</div>
<div class="grid-view">
<ul class="cls" id="data-table">
<li>
<div class="img"><a target="_blank" href="http://test.com/abcdefg"> <img src="http://img.mall.test.com/abcd.jpg"> </a></div>
<div class="title">
<a class="ico-b2c" href="http://co.test.com/content/help/2009-08-06/113746233677.html#1" target="_blank" title="??数据1!"></a><h3><a target="_blank" href="http://book.test.com/">沸?151年</a></h3>
<a class="xb-icon-small-1 no_name"></a> <a class="xb-icon-small-2 no_name"></a>
</div>
<div class="bot">
价格:<strong>75.<span class="small">00</span></strong>  
<a href="#" class="btn-im-online2" account="北京??出版室"></a><br>
</div>
</li>
<li>
<div class="img"><a target="_blank" href="http://test.com/abcdefg2"> <img src="http://img.mall.test.com/abcd2.jpg"> </a></div>
<div class="title">
<a class="ico-b2c" href="http://co.test.com/content/help/2009-08-06/113746233677.html#1" target="_blank" title="??数据2!"></a><h3><a target="_blank" href="http://book.test.com/">三国演?</a></h3>
<a class="xb-icon-small-1 no_name"></a> <a class="xb-icon-small-2 no_name"></a>
</div>
<div class="bot">
价格:<strong>255.<span class="small">00</span></strong>  
<a href="#" class="btn-im-online2" account="北京??出版室"></a><br>
</div>
</li>
...(?似的数据111)
</ul>
</div>
bf234511171 2010-10-27
  • 打赏
  • 举报
回复
学习下对证则不是很了解!
Miracle1216 2010-10-27
  • 打赏
  • 举报
回复
顶!!
yidao8848 2010-10-27
  • 打赏
  • 举报
回复
这个算不错
masterz 2010-10-27
  • 打赏
  • 举报
回复
http://www.rkcole.com/articles/swing/HTMLParser.html
Using the Swing Text HTML Parser
import javax.swing.text.html.*;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.Reader;

/**
* Extract all "img" tags from an HTML document.
*/
public class HTMLParser
{
public static void main( String[] argv ) throws Exception
{
URL url = new URL( "http://java.sun.com" );
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
Reader HTMLReader = new InputStreamReader(url.openConnection().getInputStream());
kit.read(HTMLReader, doc, 0);

// Get an iterator for all HTML tags.
ElementIterator it = new ElementIterator(doc);
Element elem;

while( elem = it.next() != null )
{
if( elem.getName().equals( "img") )
{
String s = (String) elem.getAttributes().getAttribute(HTML.Attribute.SRC);
if( s != null )
System.out.println (s );
}
}
System.exit(0);
}
}
snowbirdfly 2010-10-26
  • 打赏
  • 举报
回复
嗯.DOM树可以把这些元素组织成树状结构,不过我还是想直接用正则表达式,嘿嘿,我再看看,谢谢了!
qqyl521 2010-10-26
  • 打赏
  • 举报
回复
首先把HTML页面解析成DOM树 然后你根据对DOM树的操作来处理你想要的节点,或者通过正则或者xpath匹配你想要的东西就可以了
snowbirdfly 2010-10-26
  • 打赏
  • 举报
回复
额,谢谢楼上两位的热心回答!
jsoup,刚查了下,是个很好的HTML解析类库,等下测试用下!
解析xml的方式来解析它
这个方法不是太好,因为HTML格式和XML格式相差还是比较大的,谢谢你的回答,嘿嘿!
yktd26 2010-10-26
  • 打赏
  • 举报
回复
第一个应该没那么复杂?我觉得关键是怎么找到对应的/div
		String s = "aaa<div class=\"grid-view\">bbb";
Pattern p = Pattern.compile("<div class=\"grid-view\">");
Matcher m = p.matcher(s);
if (m.find()) System.out.print(m.group());

第二个看看能不能参考这样
		String s = "<li>aaa</li><li>bbb</li><li>ccc</li>";
Pattern p = Pattern.compile("<li>.*?</li>");
Matcher m = p.matcher(s);
while (m.find()) System.out.println(m.group());
zdjray 2010-10-26
  • 打赏
  • 举报
回复
你可以尝试用解析xml的方式来解析它,已经有现成的库和例子
huntor 2010-10-26
  • 打赏
  • 举报
回复
处理 html 使用 jsoup 比较方便。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧