比如说一网址
如:
http://open.baidu.com/static/time/beijingtime.html?time=1234类似这样的现在要抓取 “05月03日 周二”我使用 httpclient
try {
HttpClient httpclient = new DefaultHttpClient();
URI uri = URIUtils.createURI("http", "open.baidu.com", -1, "/static/time/beijingtime.html", "time=1234", null);
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if(entity!=null){
long len = entity.getContentLength();
entity = new BufferedHttpEntity(entity);
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int i = 0;
while(br.ready()){
System.out.println(i);
System.out.println(br.readLine());
i++;
}
}
可以将整个网页写出来。但是只想得到 特殊的一段,这个是遍历是怎么进行?
有什么方式可以较好的直接把我需要的东西拉出来?
如:
http://open.baidu.com/static/time/beijingtime.html?time=1234类似这样的现在要抓取 “05月03日 周二”我使用 httpclient
try {
HttpClient httpclient = new DefaultHttpClient();
URI uri = URIUtils.createURI("http", "open.baidu.com", -1, "/static/time/beijingtime.html", "time=1234", null);
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if(entity!=null){
long len = entity.getContentLength();
entity = new BufferedHttpEntity(entity);
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int i = 0;
while(br.ready()){
System.out.println(i);
System.out.println(br.readLine());
i++;
}
}
可以将整个网页写出来。但是只想得到 特殊的一段,这个是遍历是怎么进行?
有什么方式可以较好的直接把我需要的东西拉出来?
解决方案 »
- 优化java程序的瓶颈是什么?
- jxl解析excel问题
- 外企面试后发了张联络函是什么意思?
- struts1.x validator 如何只验证某个方法?
- hibernate 的事物问题. 等线等呀.
- 请问java中怎么执行一个 sql 脚本文件
- 如何使JTable的列不可移动?
- hibernate用weblogic连接池问题,急啊!!!望潜水的大侠们帮帮忙!
- Hibernate query.list() 返回很慢 要等9秒
- @SessionAttributes是什么标记?
- java.lang.NullPointerException处理
- 散分啦,,各位大侠,我将各种web标签全部整理收集了,供大家一起使用。。
将html从文字直接转为对象,然后可以像
前台那样 document.getElementsByTagName("div")[0].childNode[0].nodeValue
类似这样将 html 转为 dom对象然后处理。
怎么去做?
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>北京时间</title>
<style type="text/css">
p,dl,dd,div,h1,h2,table,td,th,ul,ol,li,img,form{margin:0;padding:0;}div,ul{zoom:1;}a:link{color:#261cdc;}img{border:none;}body{margin:6px 0 0 0;background-color:#fff;color:#000;font-family:arial;}#head{margin-left:8px;width:670px;height:54px;}#head_content{float:left;width:100%;font-size:12px;}#head_content_inner{margin:0 100px 0 146px;zoom:1;_margin-left:0;}#nav{margin:1px 0 4px;font-size:14px;}#nav a{color:#261cdc;}#kw{font-size:16px;font-family:arial;width:333px;}#logo{float:left;width:146px;_margin-right:-3px;}#gaoji{float:right;width:100px;margin-left:-100px;padding-top:26px;font-size:12px;}#tp{clear:both;font-size:12px;background-color:#d9e1f7;padding:0 12px 0 9px;height:20px;line-height:20px;margin:0 0 11px 0;}#tp a{color:#000;}#ft{line-height:20px;background:#E6E6E6;text-align:center;color:#77C;font-size:12px;width:100%;}#ft span{color:#666;}.main{clear:both;margin:20px 30px 20px 40px;position:relative;width:auto;}h1{font-size:16px;margin-bottom:12px;}th{font-weight:normal;}#bjtime h2{font-size:16px;color:#000;}#bjt{line-height:1em;}#bjt .swfBox{width:64px;height:64px;overflow:hidden;margin-right:10px;}#bjt_1{margin-top:20px;overflow:hidden;}#bjt_info{float:left;height:64px;}#swf_clock{float:left;width:0;height:0;overflow:hidden;}#time{font-size:22px;font-family:Arial;line-height:36px;}#date{font-size:13px;color:#666;line-height:24px;}#ald_icon{border:0;}#bjtime{margin-bottom:0;}#intro{margin-top:25px;}#intro .canvas{background:#F5F5F5;border:1px solid #CCC;padding:10px 20px;width:auto;overflow:hidden;margin-top:0;margin-bottom:60px;}#intro .t4{font-size:14px;color:#000;font-weight:bold;line-height:36px;}#intro p{font-size:13px;color:#000;font-weight:normal;line-height:24px;}#localTime{font-size:16px;color:#000;font-weight:bold;margin-left:20px;}
</style>
<script type="text/javascript">function h(C,A){try{C.style.behavior="url(#default#homepage)";C.setHomePage(A)}catch(B){}};</script>
</head>
<body>
<div id="head">
<div id="head_content">
<div id="logo">
<a href="http://www.baidu.com"><img src="http://www.baidu.com/img/logo-yy.gif" width="137" height="46" alt="到百度首页"></a>
</div>
<div id="head_content_inner" style="zoom:1; _margin-left:0px">
<div id="nav">
<a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=%B1%B1%BE%A9%CA%B1%BC%E4">新闻</a> <span class="fB">网页</span> <a href="http://tieba.baidu.com/f?kw=%B1%B1%BE%A9%CA%B1%BC%E4" >贴吧</a> <a href="http://zhidao.baidu.com/q?ct=17&pn=0&tn=ikaslist&rn=10&word=%B1%B1%BE%A9%CA%B1%BC%E4&fr=wwwt" >知道</a> <a href="http://mp3.baidu.com/m?tn=baidump3&ct=134217728&lm=-1&word=%B1%B1%BE%A9%CA%B1%BC%E4" >MP3</a> <a href="http://image.baidu.com/i?tn=baiduimage&ct=201326592&lm=-1&cl=2&word=%B1%B1%BE%A9%CA%B1%BC%E4" >图片</a> <a href="http://video.baidu.com/v?ct=301989888&rn=20&pn=0&db=0&s=25&word=%B1%B1%BE%A9%CA%B1%BC%E4">视频</a>
</div>
<form action="http://www.baidu.com/s" name="f">
<input type="hidden" name="bs" value="北京时间"><input type="hidden" name="sr"><input type="hidden" name="f" value="8"><input name="wd" value="北京时间" maxlength="100" id="kw"> <input type="submit" value="百度一下">
</form>
</div>
</div>
<div id="gaoji">
<a href="http://www.baidu.com/gaoji/preferences.html">设置</a> | <a href="http://www.baidu.com/gaoji/advanced.html">高级搜索</a>
</div>
</div>
<div id="tp">
<a onclick="h(this,'http://www.baidu.com')" href="#">把百度设为主页</a>
</div>
<div class="main" id="bjtime">
<h2>北京时间 - 国家授时中心标准时间</h2>
<div id="bjt" style="margin-top:8px">
<div id="bjt_1">
<div id="swf_clock"></div>
<div id="bjt_info">
<div id="time">加载中...</div>
<div id="date">加载中...</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
if (typeof console == "undefined"){
var console = {
log:function(){}
};
}
var swfLoaded = false;
var initTime = undefined;
function setFDF(){
swfLoaded = true;
//console.log(initTime);
if(initTime) setClockTime(initTime);
return true;
}
function setClockTime(time){
if (swfLoaded && swfCtrl) {
//console.log("COLK:"+time);
//try{
swfCtrl.setTime(time);
//}catch(e){}
}
}
</script>
<script src="http://www.baidu.com/js/aladdin/clock/clock.js"></script>
<script type="text/javascript">
(function(){
var week = '日一二三四五六';
var innerHtml = '{0}:{1}:{2}';
var dateHtml = "{0}月{1}日 周{2}";
var timer = 0;
var beijingTimeZone = 8;
function format(str, json){
return str.replace(/{(\d)}/g, function(a, key) {
return json[key];
});
}
function p(s) {
return s < 10 ? '0' + s : s;
}
window.baidu_time = function(timeObj){
var time = timeObj.time;
initTime = time;
if(timer != 0){
clearInterval(timer);
timer = 0;
}
show(time);
timer = setInterval(function(){
time += 1000;
show(time);
}, 1000);
}
function show(time){
var timeOffset = ((-1 * (new Date()).getTimezoneOffset()) - (beijingTimeZone * 60)) * 60000;
var now = new Date(time - timeOffset);
document.getElementById('time').innerHTML = format(innerHtml, [p(now.getHours()), p(now.getMinutes()), p(now.getSeconds())]);
setClockTime(time);
document.getElementById('date').innerHTML = format(dateHtml, [ p((now.getMonth()+1)), p(now.getDate()), week.charAt(now.getDay())]);
}
function init(){
var elm = document.createElement('SCRIPT');
elm.src = 'http://open.baidu.com/app?module=beijingtime&t=' + new Date().getTime();
document.getElementsByTagName('HEAD')[0].appendChild(elm);
setTimeout(function(){init()}, 60000);
}
init();
})();
</script>
<div class="main" id="intro">
<div class="canvas">
<p><span class="t4">时间校对</span></p>
<p>您的本地电脑时间:<span id="localTime"></span></p>
<p>标准北京时间的实时查询,与国家授时中心标准时间同步更新,确保现在北京时间的准确性。</p>
<p>便于您的对电脑、手表、手机等计时仪器进行校对调整。</p>
<p style="line-height:12px;"> </p>
<p><span class="t4">北京时间的定义</span></p>
<p>我国幅员辽阔,从西到东横跨东五、东六、东七、东八和东九五个时区。中华人民共和国成立以后,全国统一采用首都北京所在的东八时区的区时作为标准时间,称为北京时间。北京时间是东经120°经线的地方平太阳时,而不是北京的地方平太阳时。北京的地理经度为东经116°21′,因而北京时间与北京地方平太阳时相差约14.5分。北京时间比格林尼治时间(世界时)早8小时,即:北京时间=世界时+8小时</p>
</div>
</div>
<script type="text/javascript">
//eg: pattern = "yyyy-MM-dd hh:mm:ss";
Date.prototype.getFormattedDate = function(pattern){
function getFullStr(i){
return i>9?""+i:"0"+i;
}
pattern = pattern.replace(/yyyy/,this.getFullYear())
.replace(/MM/,getFullStr(this.getMonth()+1))
.replace(/dd/,getFullStr(this.getDate()))
.replace(/hh/,getFullStr(this.getHours()))
.replace(/mm/,getFullStr(this.getMinutes()))
.replace(/ss/,getFullStr(this.getSeconds()));
return pattern;
};
var localTimeSpan = document.getElementById("localTime");
localTimeSpan.innerHTML = (new Date()).getFormattedDate("hh : mm : ss");
setInterval(function(){
localTimeSpan.innerHTML = (new Date()).getFormattedDate("hh : mm : ss");
}, 1000); </script>
<div id="ft">©2011 Baidu <span>此内容系百度根据您的指令自动搜索的结果,不代表百度赞成被搜索网站的内容或立场</span></div></body></html>
假如我现在想得到这行字
<p>便于您的对电脑、手表、手机等计时仪器进行校对调整。</p>我已知的是标签的 <p id="p1" name="p1" class="p1">
这样的,我想得到不同的 uri?time=1234 uri?time=56746
返回的 <p id="p1">中的内容
或者是 第四个 <p>标签的内容所以感觉将 html.txt 转化为对象好处理。现在做的有点类似网页爬虫。
HTML Parser的文章,这个工具比较好用。
还有其它类似的开源工具么?