设为首页
收藏本站
用户名
Email
自动登录
找回密码
密码
登录
注册
快捷导航
平台
Portal
论坛
BBS
文库
项目
群组
Group
我的博客
Space
搜索
搜索
热搜:
shell
linux
mysql
本版
用户
LinuxSir.cn,穿越时空的Linuxsir!
»
论坛
›
Linux 综合讨论区 —— LinuxSir.cn
›
shell进阶应用、shell编程
›
今天写了个从腾讯网站抓取天气信息的脚本 ...
1
2
3
/ 3 页
下一页
返回列表
查看:
5621
|
回复:
25
今天写了个从腾讯网站抓取天气信息的脚本
[复制链接]
sipingal
sipingal
当前离线
积分
692
IP卡
狗仔卡
发表于 2005-12-8 18:05:05
|
显示全部楼层
|
阅读模式
用roottail可以输出到桌面。
使用方法
[color="Red"]./getweather.sh <城市代码>
不加任何参数查询的是深圳的天气(默认)。
[color="Red"]./getweather.sh 125 查询得到北京的天气
________________________________
天气 晴
温度 -7℃~1℃
风力 小于3级
紫外线 中等
空气 良
________________________________
复制代码
./getweather.sh [-V/--verison]显示版本信息
./getweather.sh [-h/--help/其他非法参数]显示帮助信息
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有帐号?
注册
x
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
显身卡
xiaoshao_0_0
xiaoshao_0_0
当前离线
积分
485
IP卡
狗仔卡
发表于 2005-12-8 21:17:28
|
显示全部楼层
好贴,等我做成desklet哈哈
回复
支持
反对
使用道具
举报
显身卡
webyuhang
webyuhang
当前离线
积分
107
IP卡
狗仔卡
发表于 2005-12-9 09:48:29
|
显示全部楼层
不错,学习ING。。。。
回复
支持
反对
使用道具
举报
显身卡
Yuri
Yuri
当前离线
积分
3231
IP卡
狗仔卡
发表于 2005-12-9 10:01:49
|
显示全部楼层
明显是给我这个死不要脸的机会麽.
代码在这里!
系统要求:
1:wget(估计大家都有)
2:perl(应该也有)说的都是废话
ctcode=${1}
showversion()
{
echo "Version 1.0 by Sipingal."
}
usage()
{
echo "usage:getweather [citycode] the default citycode is 296(深圳)"
echo " -h --help is for this screen."
echo "The citycode:"
cat << sipingal
125________北京
292________广州
252________上海
127________天津
212________重庆
115________沈阳
244________南京
211________武汉
166________成都
186________西安
82________石家庄
84________太原
189________郑州
103________长春
17________哈尔滨
69________呼和浩特
140________济南
248________合肥
287________厦门
255________杭州
276________福州
218________长沙
296________深圳
295________南宁
232________桂林
264________南昌
227________贵阳
1________香港
2________澳门
179________昆明
280________台北
150________拉萨
303________海口
57________兰州
56________西宁
78________银川
28________乌鲁木齐
sipingal
}
getcitycode()
{
true=0
for num in 125 292 252 127 212 115 244 211 166 186 82 84 189 103 17 69 140 248 287 255 276 218 296 295 232 264 227 1 2 179 280 150 303 57 56 78 28
do
{
if [ "${ctcode}" = "${num}" ]
then
true=`echo ${true} + 1 |bc`
else
continue
fi
}
done
if [ "${true}" = "1" ]
then
{
citycode=${ctcode}
getweather
printweather
}
else
{
usage
}
fi
}
printweather()
{
echo -e "天气\t\t$wheater"
echo -e "温度\t\t$temp1"
echo -e "风力\t\t$temp2"
echo -e "紫外线\t\t$temp3"
echo -e "空气\t\t$temp5"
}
getweather()
{
#http://weather.qq.com/inc/ss${citycode}.htm
wheater="`wget http://weather.qq.com/inc/ss${citycode}.htm -O /dev/stdout 2> /dev/null | tr '\t' ' ' |grep -v '^ $' |grep -v '^ $' | grep -v '^ $'| grep r_tembg5.gif | sed s'/.*"center">//'g |sed s'/<\/td>//'g`"
temp1="`wget http://weather.qq.com/inc/ss${citycode}.htm -O /dev/stdout 2> /dev/null | tr '\t' ' ' |grep -v '^ $' |grep -v '^ $' | grep -v '^ $' | perl -0 -ne 's/(tem1.gif.*)\n/$1/,print' | grep tem1.gif | sed s'/.*"center">//'g | sed s'/.*"center">//'g | sed s'/.*"center">//'g |sed s'/<\/td>//'g`"
temp2=`wget http://weather.qq.com/inc/ss${citycode}.htm -O /dev/stdout 2> /dev/null | tr '\t' ' ' |grep -v '^ $' |grep -v '^ $' | grep -v '^ $' | perl -0 -ne 's/(tem2.gif.*)\n/$1/,print' | grep tem2.gif | sed s'/.*"center">//'g | sed s'/.*"center">//'g | sed s'/.*"center">//'g |sed s'/<\/td>//'g`
temp2="`wget http://weather.qq.com/inc/ss${citycode}.htm -O /dev/stdout 2> /dev/null | tr '\t' ' ' |grep -v '^ $' |grep -v '^ $' | grep -v '^ $' | perl -0 -ne 's/(tem2.gif.*)\n/$1/,print' | grep tem2.gif | sed s'/.*"center">//'g | sed s'/.*"center">//'g | sed s'/.*"center">//'g |sed s'/<\/td>//'g`"
temp3="`wget http://weather.qq.com/inc/ss${citycode}.htm -O /dev/stdout 2> /dev/null | tr '\t' ' ' |grep -v '^ $' |grep -v '^ $' | grep -v '^ $' | perl -0 -ne 's/(tem3.gif.*)\n/$1/,print' | grep tem3.gif | sed s'/.*"center">//'g | sed s'/.*"center">//'g | sed s'/.*"center">//'g |sed s'/<\/td>//'g`"
temp5="`wget http://weather.qq.com/inc/ss${citycode}.htm -O /dev/stdout 2> /dev/null | tr '\t' ' ' |grep -v '^ $' |grep -v '^ $' | grep -v '^ $' | perl -0 -ne 's/(tem5.gif.*)\n/$1/,print' | grep tem5.gif | sed s'/.*"center">//'g | sed s'/.*"center">//'g | sed s'/.*"center">//'g |sed s'/<\/td>//'g`"
#perl -0 -ne 's/(tem1.gif.*)\n/$1/,print'
}
# main
if [ "${ctcode}" = "" ]
then
{
citycode=296
getweather
printweather
}
elif [ "${ctcode}" = "-H" ]
then
{
usage
}
elif [ "${ctcode}" = "--help" ]
then
{
usage
}
elif [ "${ctcode}" = "-V" ]
then
{
showversion
}
elif [ "${ctcode}" = "--version" ]
then
{
showversion
}
else
{
getcitycode
}
fi
复制代码
回复
支持
反对
使用道具
举报
显身卡
troll
troll
当前离线
积分
3053
IP卡
狗仔卡
发表于 2005-12-9 10:06:22
|
显示全部楼层
wget http://weather.qq.com/inc/ss${citycode}.htm -O - 2>/dev/null
复制代码
回复
支持
反对
使用道具
举报
显身卡
kayue
kayue
当前离线
积分
75
IP卡
狗仔卡
发表于 2005-12-11 22:24:40
|
显示全部楼层
分析这个页面用不着perl,直接用grep,sed等就已经够了,
而且不必下载五次这个页面,太浪费时间了,下面是我改写的函数,
把城市的citycode传给该函数即可:
GetWeatherInfo()
{
#如果已经存在该页面,先删除
if [ test -f ss$1.htm ]
then
rm ss$1.htm
fi
#下载页面
wget http://weather.qq.com/inc/ss$1.htm >/dev/null 2>&1
#测试下载成功没有
if [ test -f ss$1.htm ]
then
echo "获取天气页面出错...可能该城市的天气报告页面不存在.."
exit 1
fi
#分析页面
weather= `cat ss${1}.htm |grep "/images/r_tembg5.gif"|sed 's/.*"center">//g' |sed 's/<\/td>//'`
temperature= `cat ss${1}.htm|sed -n '/tem1.gif/{N;p}'|sed '1d'|sed 's/.*center">//g'`
windforce=`cat ss${1}.htm|sed -n '/tem2.gif/{N;p}'|sed '1d'|sed 's/.*"center">//g' |sed 's/<\/td>//'`
ultraviolet=`cat ss${1}.htm|sed -n '/tem3.gif/{N;p}'|sed '1d'|sed 's/.*"center">//g' |sed 's/<\/td>//'`
air= `cat ss${1}.htm|sed -n '/tem5.gif/{N;p}'|sed '1d'|sed 's/.*"center">//g' |sed 's/<\/td>//'`
#打印相关信息
echo -e "天气\t\t$weather"
echo -e "温度\t\t$temperature"
echo -e "风力\t\t$windforce"
echo -e "紫外线\t\t$ultraviolet"
echo -e "空气\t\t$air"
#最后删除页面
rm ss$1.htm
}
复制代码
另外,这种方法无法得到空气情况,不爽。。。但weather.qq.com主页是可以正确显示的。。
回复
支持
反对
使用道具
举报
显身卡
huan
huan
当前离线
积分
462
IP卡
狗仔卡
发表于 2005-12-13 21:28:45
|
显示全部楼层
[No.539 21:32:52 bash ]# cat myweather.sh
#! /bin/bash
# 修改了处理过程和优化了速度
# 经过修改已经支持城市代码和城市名
# 支持同时查询多个城市天气的查询
city_code_file="/linux/xuexi/bash/code_city.db"
cd /tmp
usage()
{
echo "Usage: `basename $0` [codes/citys] "
echo
cat << CITY
1 香港 2 澳门 17 哈尔滨 28 乌鲁木齐 56 西宁
57 兰州 69 呼和浩特 78 银川 82 石家庄 84 太原
103 长春 115 沈阳 125 北京 127 天津 140 济南
150 拉萨 166 成都 179 昆明 186 西安 189 郑州
211 武汉 212 重庆 218 长沙 227 贵阳 232 桂林
244 南京 248 合肥 252 上海 255 杭州 264 南昌
276 福州 280 台北 287 厦门 292 广州 295 南宁
296 深圳 303 海口
CITY
echo
exit 0
}
getweather()
{
grep "<td height="23"" ss.html > ss.txt
city=$(grep "<td height="23"" ss.txt | sed -n '1p' | sed 's/[[:lower:][:digit:]\<\>="\/_\. ]//g')
weat=$(grep "<td height="23"" ss.txt | sed -n '2p' | sed 's/.*center">//' | sed 's/<.*//')
temp=$(grep "<td height="23"" ss.txt | sed -n '4p' | sed 's/.*>//')
wind=$(grep "<td height="23"" ss.txt | sed -n '6p' | sed 's/.*center">//' | sed 's/<.*//')
viol=$(grep "<td height="23"" ss.txt | sed -n '8p' | sed 's/.*center">//' | sed 's/<.*//')
aire=$(grep "<td height="23"" ss.txt | sed -n '10p' | sed 's/.*center">//' | sed 's/<.*//')
}
getall()
{
wget -O /tmp/ss.html http://weather.qq.com/inc/ss${code}.htm > /dev/null 2>&1
getweather
showweather
rm -f ss.html ss.txt > /dev/null 2>&1
}
showweather()
{
echo
echo -e "\t城 市: $city"
echo -e "\t天 气: $weat"
echo -e "\t温 度: $temp"
echo -e "\t风 力: $wind"
echo -e "\t空 气: $aire"
echo -e "\t紫外线: $viol"
echo
}
####### MAIN #########
[[ $1 == "-h" ]] || [[ $1 == "--help" ]] && usage
if [[ -z $1 ]];then
set 103 303
fi
for citycode in "$@";do
code=$(grep -w "$citycode" $city_code_file | awk '{print $1}')
if [[ -n $code ]];then
getall
else
echo "$citycode not invalid."
usage
fi
done
exit 0
复制代码
城市名和城市代码数据库
[No.540 21:33:05 bash ]# cat code_city.db
1 香港
2 澳门
17 哈尔滨
28 乌鲁木齐
56 西宁
57 兰州
69 呼和浩特
78 银川
82 石家庄
84 太原
103 长春
115 沈阳
125 北京
127 天津
140 济南
150 拉萨
166 成都
179 昆明
186 西安
189 郑州
211 武汉
212 重庆
218 长沙
227 贵阳
232 桂林
244 南京
248 合肥
252 上海
255 杭州
264 南昌
276 福州
280 台北
287 厦门
292 广州
295 南宁
296 深圳
303 海口
复制代码
回复
支持
反对
使用道具
举报
显身卡
myangel
myangel
当前离线
积分
43
IP卡
狗仔卡
发表于 2005-12-14 08:48:28
|
显示全部楼层
楼主的桌面背景好PP,能否分享?
回复
支持
反对
使用道具
举报
显身卡
ajinn
ajinn
当前离线
积分
188
IP卡
狗仔卡
发表于 2005-12-14 09:42:53
|
显示全部楼层
弱弱的问一下,怎样查询到自己城市的代码
回复
支持
反对
使用道具
举报
显身卡
threedot
threedot
当前离线
积分
331
IP卡
狗仔卡
发表于 2005-12-14 13:29:33
|
显示全部楼层
弱弱的问一下,root-tail用哪个中文字体啊?我的不能显示中文
回复
支持
反对
使用道具
举报
显身卡
下一页 »
1
2
3
/ 3 页
下一页
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
注册
本版积分规则
发表回复
回帖后跳转到最后一页
浏览过的版块
LFS(LinuxfromScratch)
Slackware Linux
Archlinux讨论区
Redhat/Fedora/CentOS Linux
Mandriva Linux
Linux 基础讨论区(新手速成)
Ubuntu Linux 专题讨论
服务器架设、应用、维护
Debian Linux
Copyright © 2002-2023
LinuxSir.cn
(http://www.linuxsir.cn/) 版权所有 All Rights Reserved.
Powered by
RedflagLinux!
技术支持:
中科红旗
|
京ICP备19024520号
快速回复
返回顶部
返回列表