获取天气html,使用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;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542968.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用gulp构建前端(三)

为什么80%的码农都做不了架构师&#xff1f;>>> 使用gulp构建前端&#xff08;三&#xff09; 紧接着上述文章内容&#xff0c;开始新的插件的使用 插件三 gulp-clean-css&#xff0c;作用减小文件大小&#xff0c;并给引用url添加版本号避免缓存&#xff0c;一个需…

c++中atan2函数_atan2()函数以及C ++中的示例

c中atan2函数C atan2()函数 (C atan2() function) atan2() function is a library function of cmath header, it is used to find the principal value of the arc tangent of y/x, where y is the proportion of the y-coordinate and x is the proportion of the x-coordin…

python不合法的布尔表达式_python测试题

一、填空题 1.Python使用符号标示注释;以划分语句块。 2、Python序列类型包括 、、三种; 是Python中唯一的映射类型。 3、Python中的可变数据类型有,不可变数据类 型有。 4、Python的数字类型分为、、等子类型。 5、设s=‘abcdefg’,则s[3]值是 ,s[3:5]值是 , s[:5]值是…

网站html标签如何优化,网站HTML标签优化教程

真正需要SEOer掌握并经常使用的HTML标签也就那么几个&#xff0c;本小节丈哥seo带来的是页面HTML标签优化。title标签 – 标题标签1)title标签用法网站SEOHTML标签优化教程 – 丈哥seo2)标题标签的作用关于网站标题写法与操作技巧有许多&#xff0c;需要怎么优化标题的朋友可以…

hypot函数_hypot()函数与C ++中的示例

hypot函数C hypot()函数 (C hypot() function) hypot() function is a library function of cmath header, it is used to find the hypotenuse of the given numbers, it accepts two numbers and returns the calculated result of hypotenuse i.e. sqrt(x*x y*y). hypot()…

docker 磁盘问题:device or resource busy

问题&#xff1a;[roothdp-gp-dk02 ~]# docker start 01ea65845e45Error response from daemon: Cannot start container 01ea65845e45: Error getting container 01ea65845e4547be24bc3db0bc3b424faf52b8a0534eb525aa829dd9fd6081c2 from driver devicemapper: Error mounting…

axure form列表_常见的列表页如何用Axure画出来

列表页是PM画原型的时候最容易碰到的页面类型&#xff0c;那么如何快速画出并且展现交互呢&#xff1f;我们从具体的例子开始讲解其中的技巧&#xff0c;和各种不同的演变场景。希望初级PM可以通过这篇文章改进下自己的画法。列表页基本是由页面框架&#xff0c;上导航&#xf…

python中divmod_divmod()函数以及Python中的示例

python中divmodPython divmod()函数 (Python divmod() function) divmod() function is a library function, it is used to get the quotient and remainder of given values (dividend and divisor), it accepts two arguments 1) dividend and 2) divisor and returns a tup…

html 轮播图的鼠标事件,这是图片轮播的代码 html+css,怎么加上鼠标悬停移出继续功能?...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼.one {position: absolute;width: 500px;height: 400px;overflow: hidden;}.one_cantent img {width: 500px;height: 300px;float: left;}.one_cantent {width: 2500px;height: 400px;position: absolute;left: 0px;animation-name…

微软Azure云之企业Exchange 2016部署12—配置负载平衡

上几节我们把Exchange2016邮箱系统配置完毕了&#xff0c;本节我们来配置开通需要对外的端口&#xff0c;并对这些端口进行负载均衡配置&#xff0c;如下图红框所示&#xff1a;1.Exchange服务端口Exchange2016需要对外开通以下的端口&#xff0c;来对外提供不同的服务&#xf…

linux多线程求和_linux多线程pthread系列函数详解

linux多线程pthread系列函数详解(一)为什么要引入线程线程技术早在60年代就被提出&#xff0c;但是在80年代才真正使用到操作系统中。传统UNIX也支持多线程概念&#xff0c;但在一个进程中只允许有一个线程&#xff0c;这样多线程就意味着多进程。现在多线程技术已经被很多操作…

python独立log示例_带有Python示例的math.log()方法

python独立log示例Python math.log()方法 (Python math.log() method) math.log() method is a library method of math module, it is used to get the natural logarithm of a given number to base e (by default). It accepts the number and a base (which an optional) a…

搭建云计算机win10,win10电脑做云服务器

弹性云服务器 ECS弹性云服务器(Elastic Cloud Server)是一种可随时自助获取、可弹性伸缩的云服务器&#xff0c;帮助用户打造可靠、安全、灵活、高效的应用环境&#xff0c;确保服务持久稳定运行&#xff0c;提升运维效率三年低至5折&#xff0c;多种配置可选了解详情Windows远…

聚类分析在用户行为中的实例_用户关注行为数据分析过程详解-描述统计+聚类...

以下是基于一小段用户关注产品的行为数据分析过程详细描述&#xff0c;各位老师如有不同意见或优化建议&#xff0c;还请不吝赐教。一、数据大致是这样的(已经脱敏处理)二、数据预处理从年款提取出年限&#xff0c;从价格标识出价格区间&#xff0c;随便写一下SQL如下&#xff…

centos下升级jdk版本

2019独角兽企业重金招聘Python工程师标准>>> 由于安装的centos系统&#xff0c;自带了jdk1.6和1.7的版本&#xff0c;介于在平时开发&#xff0c;用了1.8.为了保持开发环境的一致&#xff0c;故此需要升级&#xff0c;放办maven打包等工具的使用。 查看系统的jdk版本…

学英语要用计算机吗,学计算机的人是否要学英语?

我记得在我大一的时候,那个时候还没有程序课呢.不过我是很喜欢计算机这个专业的,我也非常想学习编程语言,所以当时即使没有程序课,我也是借了一本VB的书回家来看.当时就在VB的贴吧乱看,大概贴吧里有一个人气很高的一位高手,写了好多文章,也帮人解决好多问题.在他的观点中,他认为…

语言之输出png图片_什么是PPML?如何输出PPML文档?

什么是PPML&#xff1f;PPML是由PODi联盟的几个会员公司共同开发出一种个性化印刷标识语言&#xff0c;这是一种全新的、基于XML(可扩展标识语言&#xff09;技术的印刷行业标准语言&#xff0c;得到越来越多的业内人士关注。PPML采用XML作为句法基础&#xff0c;使PPML其他很多…

TCP/IP协议网络模型

1.tcp/ip分层模型 tcp/ip把网络结构分为四层&#xff0c;从上到下依次为&#xff1a;应用层、传输层、互联网层、网络接口层。每层担任不同的职责&#xff0c;下层服务于上层&#xff0c;最终上层就是用户使用的层。分层结构如图1所示。 图1 tcp/ip分层模型 (1)应用层 向用户提…

c++中函数atan_atan()函数以及C ++中的示例

c中函数atanC atan()函数 (C atan() function) atan() function is a library function of cmath header, it is used to find the principal value of the arc tangent of the given number, it accepts a number (x) and returns the principal value of the arc tangent of…

我想成为计算机专业第一,我对计算机专业学生的忠告。

计算机专业的学生在面临毕业的时候有些同学应该会有这样的想法&#xff0c;我对计算机没有兴趣&#xff0c;毕业不打算从事计算机专业的对口工作&#xff1b;或者我当时选择学计算机专业是因为那时候对计算机有比较大的兴趣&#xff0c;而现在&#xff0c;越来越觉得没有意思了…