最近获取网页页面,获取该网页页面的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以前没怎么用过正则表达式,搜索看了很多资料,始终未找到和此应用相关的例子,请求高手解答下!
谢谢!!!
...(中间部分省略)
<!-- {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以前没怎么用过正则表达式,搜索看了很多资料,始终未找到和此应用相关的例子,请求高手解答下!
谢谢!!!
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());
jsoup,刚查了下,是个很好的HTML解析类库,等下测试用下!
解析xml的方式来解析它
这个方法不是太好,因为HTML格式和XML格式相差还是比较大的,谢谢你的回答,嘿嘿!
http://htmlparser.sourceforge.net/
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>
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);
哪位大侠指点下!
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);
}
}