如何从网上看动提取文本信息? 如题.现已知道Html2Xml、jspider等可以实现此功能,并能满足要求。但是不知道这些东西具体该怎么用。如果能给出实例最好!急!急!急!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以用htmlparser,它的使用案例比较多,网上可以很容易搜索到,一下是获取sohu天气预报的例子,希望能帮到lzpackage 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; } Thank you!不过很可惜,没通过编译.html2xml包我已经下载下来了. Thank you!不过很可惜,没通过编译.htmlparser包我已经下载下来了 新手求教一道面试题,只需要写出接口和实现接口的框架就好,第一题也麻烦看下 问个傻瓜问题 C#、C++和Java中重载和覆盖的一点问题 在窗口中绘图以后,想通过clear按扭清除画的曲线,怎么处理 如何把数据库里面的字符取出来,连成表达式,算出来呢 请大虾帮我看看这句 InputStream is = getClass().getResourceAsStream("/db.properties"); 菜鸟问题 一个简单编译问题 那里能下载db2 JAVA 生产与消费的程序 同步问题 while为什么不能换成if 怎么实现这样的多态 一道面试逻辑思维题
* @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;
}
不过很可惜,没通过编译.
html2xml包我已经下载下来了.
不过很可惜,没通过编译.
htmlparser包我已经下载下来了