62,614
社区成员
发帖
与我相关
我的任务
分享
<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>百度一下,你就知道 </title><style>body{font:12px arial;text-align:center;background:#fff}body,p,form,ul{margin:0;padding:0}body,form,#fm,#lg img{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{padding:7px 10px 3px 0;text-align:right}#m{width:650px;margin:0 auto}#nv{font-size:16px;margin:0 0 4px -51px}#nv a,#nv b,#su,#lk{font-size:14px}#lg{margin:-17px 0 9px}#fm{padding-left:100px;text-align:left}#kw{width:391px;padding:3px 1px;margin:0 6px 0 0;font:16px/16px arial}#su{width:78px;height:28px;line-height:24px}#kw,#su{vertical-align:middle}#lk{margin:33px 0}#lk span{font:14px "宋体"}#lm{height:60px}#lh{margin:16px 0 5px;font:12px "宋体"}#lh a{font:12px arial}#mCon{position:absolute;right:7px;*top:9px;top:5px;cursor:pointer;padding:0 18px 0 0;background:url(http://www.baidu.com/img/arr.gif) no-repeat right center}#mCon span{color:#00c;cursor:default;display:block;padding-top:3px}#mCon .hw{text-decoration:underline;cursor:pointer}#mMenu{width:56px;border:1px solid #9a99ff;list-style:none;position:absolute;right:7px;top:28px;display:none;background:#fff}#mMenu a{width:100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:none}#mMenu a:hover{background:#d9e1f6}#mMenu .ln{height:1px;background:#ccf;overflow:hidden;margin:2px;font-size:1px;line-height:1px}#cp,#cp a{color:#77c}</style></head><body><p id="u"><a href="/gaoji/preferences.html">搜索设置</a> | <a href="http://passport.baidu.com/?login&tpl=mn">登录</a></p><div id="m"><p id="lg"><img src="http://www.baidu.com/img/baidu_logo.gif" width="270" height="129" usemap="#mp"></p><p id="nv"><a href="http://news.baidu.com">新 闻</a> <b>网 页</b> <a href="http://tieba.baidu.com">贴 吧</a> <a href="http://zhidao.baidu.com">知 道</a> <a href="http://mp3.baidu.com">MP3</a> <a href="http://image.baidu.com">图 片</a> <a href="http://video.baidu.com">视 频</a> <a href="http://map.baidu.com">地 图</a></p><div id="fm"><form name="f" action="s"><input type="text" name="wd" id="kw" maxlength="100"><input type="submit" value="百度一下" id="su"></form><div id="mCon"><span>输入法</span></div><ul id="mMenu"><li><a href="#" name="ime_hw">手写</a></li><li><a href="#" name="ime_py">拼音</a></li><li class="ln"></li><li><a href="#" name="ime_cl">关闭</a></li></ul></div><p id="lk"><a href="http://hi.baidu.com">空间</a> <a href="http://baike.baidu.com">百科</a> <a href="http://www.hao123.com">hao123</a><span> | <a href="/more/">更多>></a></span></p><p id="lm"></p><p><a id="st" onClick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.baidu.com')" href="http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com">把百度设为主页</a></p><p id="lh"><a href="http://e.baidu.com/?refer=888">加入百度推广</a> | <a href="http://top.baidu.com">搜索风云榜</a> | <a href="http://home.baidu.com">关于百度</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">©2011 Baidu <a href="/duty/">使用百度前必读</a><a href="http://www.miibeian.gov.cn" target="_blank">京ICP证030173号</a><img src="http://gimg.baidu.com/img/gs.gif"></p></div><map name="mp"><area shape="rect" coords="43,22,227,91" href="http://hi.baidu.com/baidu/" target="_blank" title="点此进入 百度的空间"></map></body></html><!--d6896c28c03e0c49-->
str = str.replaceAll("(?<=\\>)(?:\\s*\r?\n?)(?=\\<)","");
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class Test {
public static void main(String[] args) throws Exception{
StringBuffer document = new StringBuffer();
try
{
URL url = new URL("http://www.csdn.net");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document = document.append(line + "\r\n");
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(document.toString());
System.out.println(lostGaps(document.toString()));
}
public static String lostGaps(String content){
String regex = "(.+?>)\\s+\n?(<(.|\n)*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
//System.out.println(matcher.find());
while(matcher.find()){
//System.out.println("get here");
content = matcher.group(1) + matcher.group(2);
matcher = pattern.matcher(content);
}
return content;
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
public class TestParser {
public static void main(String[] args) throws Exception{
StringBuffer document = new StringBuffer();
try
{
URL url = new URL("http://www.csdn.net");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + " ");
reader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(lostGaps(document.toString()));
}
public static String lostGaps(String content){
String regex = "(.+?>)\\s+\n?(<(.|\n)*)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
//System.out.println(matcher.find());
while(matcher.find()){
//System.out.println("get here");
content = matcher.group(1) + matcher.group(2);
matcher = pattern.matcher(content);
}
return content;
}
}