首先,我们新建一个类,存放天气信息

/*

* 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;

}

获取天气html,使用htmlparser获取sohu的天气预报相关推荐

  1. python发送文件给微信好友_python获取天气接口给指定微信好友发天气预报

    先看下效果图: 用到的模块: PyMySQL requests threading wxpy 要实现上面的示例,首先是有两大块地方 获取天气信息 通过微信将天气信息发送出去 而获取天气信息又包括几个小 ...

  2. python获取天气分析_Python爬取南京市往年天气预报,使用pyecharts进行分析

    上一次分享了使用matplotlib对爬取的豆瓣书籍排行榜进行分析,但是发现python本身自带的这个绘图分析库还是有一些局限,绘图不够美观等,在网上搜索了一波,发现现在有很多的支持python的绘图 ...

  3. python自动获取天气_用python获取天气数据,并作定时播报

    原标题:用python获取天气数据,并作定时播报 数据挖掘入门与实战 公众号: datadw 思路 1.调用和风天气的API,获取天气数据 2.用百度语音API,将天气数据合成语音 3.用树莓派每天早 ...

  4. php通过api获取天气信息,调用API获取城市天气信息

    Code: /* 调用API获取指定城市的天气数据,并输出 */ echo ""; $url="http://m.weather.com.cn/data/10121010 ...

  5. android 定时刷新获取天气信息,android中获取即时天气

    先看效果: 需求非常简单,在菜单中加入天气查询的按钮,点击后显示即时天气. 准备工作: 1.下载华为能力SDK:http://imax.vmall.com/nj-campus/universityEp ...

  6. ESP8266 WIFI模块获取天气信息

    ESP8266 WIFI模块获取天气信息 基本步骤 获取天气时间 基本步骤 (1)确认模块在 STA 模式. 如果不在需要输入AT指令: AT+CWMODE=1 响应OK后输入: AT+RST或者重新 ...

  7. 用树莓派获取天气状况

    用树莓派获取天气状况 在树莓派上我们可以通过"wether"工具来以命令行的方式获取天气预报信息. 首先当然是安装Weather工具,通过命令行 sudo apt-get inst ...

  8. android获取天气信息_像素小天气

    像素小天气是一个非常美观有趣的天气预报app,市面上天气软件种类繁多,但是这款像素小天气可以说是让小编眼前一亮,顿时心生欢喜.软件内所有的图标都是像素风格的,看起来非常的可爱,并且首页壁纸是获取的每日 ...

  9. android往天气接口里面传城市,Android使用中国天气网API数据通过城市名称获取天气情况...

    项目要求在应用首页面展示本地当日天气的概况,首先想到的是google和雅虎,前者很久之前接触过,听说后来用不了了,后者由于邮箱事件的缘故个人不喜欢(虽然貌似苹果也用雅虎的天气预报),之后又想到了前段时 ...

最新文章

  1. Vue.js 生命周期
  2. 如何优雅的使用 phpStorm 开发工具
  3. 《你不知道的JavaScript》整理(五)——值与原生函数
  4. 从浏览器市场份额,探究百度在移动互联网时代失败的原因
  5. 电脑上有一个程序一直在按向上_HUAWEI Matebook 九个超牛电脑快捷键
  6. iis7php怎么301重定向,iis7/8设置网站301重定向的方法
  7. HBase基本操作-java api
  8. Lync Server 2013企业版部署系列之三:CA准备
  9. mysql(mariadb)的安装与使用,mysql相关命令,mysql数据类型
  10. 【原创】纯CSS 仿U9论坛 箭形导航栏 兼容IE6+IE7+FF
  11. 公钥 私钥_比特币私钥、公钥、钱包地址之间的关系
  12. (附源码)基于Android的订餐app 毕业设计 190711
  13. Mathtype使用技巧
  14. 英雄无敌3pc移植android版,英雄无敌3手机版
  15. centos 7.7.1908上隐藏顶栏和任务栏
  16. pdn阻抗测试_非常详细的阻抗测试基础知识
  17. 计算机硬盘找不到,电脑找不到硬盘怎么办
  18. 微信小程序开发 自定义按钮实现分享转发功能
  19. GlobalSign是什么,其中的ssl证书类型有哪些
  20. Windows下Python新手安装教程

热门文章

  1. 联想拯救者y7000加内存条_短测联想拯救者Y7000,到底值不值得买?
  2. TensorFlow实验(3)
  3. 语言爬虫字段为空_我为什么建议前端将Python 作为第二语言?
  4. 常用的loss函数,以及在训练中的使用
  5. 日常问题——Mac下新建目录报Read-only file system
  6. 360 再次开源管理平台 Wayne:基于企业级 Kubernetes 集群
  7. 【初学者必读】:前端工程师的知识体系
  8. NHibernate利用Mindscape.NHibernateModelDesigner实现数据库与实体之间的转换及操作
  9. intellij idea 最常用的快捷键
  10. 成为项目经理需要具备什么条件?