怎样从特定网页的特定位置截取数据? 挂上几天了,怎么没人说话啊?各位大虾不能见死不救啊!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 表中套表,用正则式太复杂了啊?有没有别的方法啊? 给你个参考,希望有用啊! 使用htmlparser获取sohu的天气预报首先,我们新建一个类,存放天气信息/* * Created on 2005-3-8 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package org.exoplatform.portlets.chinaweather.component;/** * @author Administrator * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class Weather { private String city; private String state; private String temperature; private String time; private String wind; private String windpower; private long UpdateTime; /** * @return */ public String getTemperature() { return temperature; } /** * @return */ public String getTime() { return time; } /** * @return */ public String getWind() { return wind; } /** * @return */ public String getWindpower() { return windpower; } /** * @param string */ public void setTemperature(String string) { temperature = string; } /** * @param string */ public void setTime(String string) { time = string; } /** * @param string */ public void setWind(String string) { wind = string; } /** * @param string */ public void setWindpower(String string) { windpower = string; } /** * @return */ public long getUpdateTime() { return UpdateTime; } /** * @param l */ public void setUpdateTime(long l) { UpdateTime = l; } /** * @return */ public String getState() { return state; } /** * @param string */ public void setState(String string) { state = string; } /** * @return */ public String getCity() { return city; } /** * @param string */ public void setCity(String string) { city = string; }}具体的解析代码为:private Weather parserWeather() throws Exception { Weather w = new Weather(); try { //Parser parser = // new Parser("file://localhost/I:/projects/query.html"); Parser parser = new Parser("http://weather.news.sohu.com/query.php?city=镇江"); parser.setEncoding("GBK"); Node nodes[] = parser.extractAllNodesThatAre(TableTag.class); TableTag table = (TableTag) nodes[3]; //temperature StringNode[] stringNodes = table.digupStringNode("镇江"); StringNode name = stringNodes[0]; w.setCity(name.toPlainTextString()); CompositeTag td = (CompositeTag) name.getParent(); CompositeTag tr = (CompositeTag) td.getParent(); int columnNo = tr.findPositionOf(td); TableColumn nextColumn = (TableColumn) tr.childAt(5); Node expectedName = nextColumn.childAt(0); Node expectedName2 = nextColumn.childAt(2); //System.out.println(expectedName.getText()); //System.out.println(expectedName2.getText()); w.setState(expectedName.getText()); w.setTemperature(expectedName2.getText()); //time stringNodes = table.digupStringNode("时间"); name = stringNodes[0]; //System.out.println(name.toPlainTextString()); String time = name .toPlainTextString() .substring(4, name.toPlainTextString().length()) .trim(); //System.out.println(time); w.setTime(time); //wind stringNodes = table.digupStringNode("风向"); name = stringNodes[0]; //System.out.println(name.toPlainTextString()); String wind = name .toPlainTextString() .substring(4, name.toPlainTextString().length()) .trim(); //System.out.println(wind); w.setWind(wind); //wind power stringNodes = table.digupStringNode("风力"); name = stringNodes[0]; //System.out.println(name.toPlainTextString()); String windpower = name .toPlainTextString() .substring(4, name.toPlainTextString().length()) .trim(); //System.out.println(windpower); w.setWindpower(windpower); w.setUpdateTime(System.currentTimeMillis()); } catch (ParserException e) { e.printStackTrace(); } return w; }解析出来的代码必须做缓存处理,private static long TIME_TO_LIVE = 1000 * 60 * 60 * 12; private Weather loadWeather() throws Exception { Weather weather = weather = (Weather) cache_.get("chinaweather"); long currentTime = System.currentTimeMillis(); if (weather != null && currentTime < (weather.getUpdateTime() + TIME_TO_LIVE)) { cache_.remove("chinaweather"); weather = null; } if (weather == null) { synchronized (cache_) { weather = parserWeather(); cache_.put("chinaweather", weather); } } return weather; } 楼上的缓存弄的不错, 我就直接得到网页对象了! 然后截取! Javascript在不同浏览器及动、静态网页里显示的问题 帮我看看下面代码哪错了::: 如何设置模式窗口状态栏得内容? 请教,如何实现全局变量? 怎样判断复选框的选中状态? 请问如何取到设置好的cookies值?怎么写判断此cookies是否为空? 在c中嵌入javascript的问题! 如何用javascript 控制页面设置中的页眉、页脚? 滚动文字的问题 用JS实现图片分割,拼接 如何实现 点击flash的链接出现 判断保存 或 直接观看的对话框? 怎么删除关联数组中的元素??
首先,我们新建一个类,存放天气信息/*
* Created on 2005-3-8
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package org.exoplatform.portlets.chinaweather.component;/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Weather {
private String city;
private String state;
private String temperature;
private String time;
private String wind;
private String windpower;
private long UpdateTime; /**
* @return
*/
public String getTemperature() {
return temperature;
} /**
* @return
*/
public String getTime() {
return time;
} /**
* @return
*/
public String getWind() {
return wind;
} /**
* @return
*/
public String getWindpower() {
return windpower;
} /**
* @param string
*/
public void setTemperature(String string) {
temperature = string;
} /**
* @param string
*/
public void setTime(String string) {
time = string;
} /**
* @param string
*/
public void setWind(String string) {
wind = string;
} /**
* @param string
*/
public void setWindpower(String string) {
windpower = string;
} /**
* @return
*/
public long getUpdateTime() {
return UpdateTime;
} /**
* @param l
*/
public void setUpdateTime(long l) {
UpdateTime = l;
} /**
* @return
*/
public String getState() {
return state;
} /**
* @param string
*/
public void setState(String string) {
state = string;
} /**
* @return
*/
public String getCity() {
return city;
} /**
* @param string
*/
public void setCity(String string) {
city = string;
}}
具体的解析代码为:private Weather parserWeather() throws Exception {
Weather w = new Weather();
try { //Parser parser =
// new Parser("file://localhost/I:/projects/query.html");
Parser parser =
new Parser("http://weather.news.sohu.com/query.php?city=镇江");
parser.setEncoding("GBK");
Node nodes[] = parser.extractAllNodesThatAre(TableTag.class); TableTag table = (TableTag) nodes[3];
//temperature
StringNode[] stringNodes = table.digupStringNode("镇江");
StringNode name = stringNodes[0];
w.setCity(name.toPlainTextString());
CompositeTag td = (CompositeTag) name.getParent();
CompositeTag tr = (CompositeTag) td.getParent();
int columnNo = tr.findPositionOf(td);
TableColumn nextColumn = (TableColumn) tr.childAt(5);
Node expectedName = nextColumn.childAt(0);
Node expectedName2 = nextColumn.childAt(2);
//System.out.println(expectedName.getText());
//System.out.println(expectedName2.getText());
w.setState(expectedName.getText());
w.setTemperature(expectedName2.getText());
//time
stringNodes = table.digupStringNode("时间");
name = stringNodes[0];
//System.out.println(name.toPlainTextString()); String time =
name
.toPlainTextString()
.substring(4, name.toPlainTextString().length())
.trim();
//System.out.println(time);
w.setTime(time);
//wind
stringNodes = table.digupStringNode("风向");
name = stringNodes[0];
//System.out.println(name.toPlainTextString()); String wind =
name
.toPlainTextString()
.substring(4, name.toPlainTextString().length())
.trim();
//System.out.println(wind);
w.setWind(wind);
//wind power
stringNodes = table.digupStringNode("风力");
name = stringNodes[0];
//System.out.println(name.toPlainTextString()); String windpower =
name
.toPlainTextString()
.substring(4, name.toPlainTextString().length())
.trim();
//System.out.println(windpower);
w.setWindpower(windpower); w.setUpdateTime(System.currentTimeMillis()); } catch (ParserException e) { e.printStackTrace();
}
return w;
}
解析出来的代码必须做缓存处理,private static long TIME_TO_LIVE = 1000 * 60 * 60 * 12; private Weather loadWeather() throws Exception {
Weather weather = weather = (Weather) cache_.get("chinaweather");
long currentTime = System.currentTimeMillis();
if (weather != null
&& currentTime < (weather.getUpdateTime() + TIME_TO_LIVE)) {
cache_.remove("chinaweather");
weather = null;
} if (weather == null) {
synchronized (cache_) {
weather = parserWeather();
cache_.put("chinaweather", weather);
}
} return weather;
}