一个天气预报的脚本

sh/bash/dash/ksh/zsh等Shell脚本
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#16

帖子 bones7456 » 2007-09-09 17:34

Beetle 写了:多个城市代码是用什么隔开?
:shock: :shock: 目前不支持多个城市代码的,有这个需求吗?那就自己改下啦, :lol: :lol:
关注我的blog: ε==3
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#17

帖子 laborer » 2007-09-10 1:34

一个根据ip地址获得当地天气状况的python程序,如下例:

代码: 全选

$ python weather.py forum.ubuntu.org.cn
东莞天气: 今天: 26℃~33℃, 上午: 多云, 下午: 阴转多云,雷雨, 晚上: 阴转多云,阵雨
明天: 26℃~31℃, 上午: 阴转多云,小阵雨, 下午: 阴转多云,雷雨, 晚上: 阴转多云,雷雨
这个程序使用QQWry.Dat(纯真IP数据库)来解析ip地址所在城市。读取这个数据库的程序qqwry.py可以在这里找到。从weather.265.com读取天气信息的部分参考了bones7456zhan的实现。

代码: 全选

# coding: utf8

import socket, re, sys, urllib


def init():
    global locate
    global re_address
    global citycode

    from qqwry import IpLocater, string2ip

    getaddr = IpLocater("QQWry.Dat").getIpAddr
    locate = lambda addr: getaddr(string2ip(socket.gethostbyname(addr))
                                  ).decode("gbk").encode("utf8")
    
    re_address = re.compile("""

(?:(?P<province>
安徽 | 北京 | 重庆 | 福建 | 甘肃 | 广东 | 广西 | 贵州 | 
海南 | 河北 | 河南 | 黑龙江 | 湖北 | 湖南 | 吉林 | 江苏 | 
江西 | 辽宁 | 内蒙古 | 宁夏 | 青海 | 山东 | 山西 | 陕西 | 
上海 | 四川 | 台湾 | 天津 | 西藏 | 新疆 | 云南 | 浙江 | 
香港 | 澳门 )
(?:省|市|(?![^ /()]*大学))
)?

(?:(?P<city>
[^ /()]+?(?=市) | 
(?<=内蒙古)[^ /()]+
)(?:市)?)?

(?P<region>[^ /()]+?(?:区|县|大学|学院))?

""", re.VERBOSE)

    conn = urllib.urlopen("http://weather.265.com")
    str = conn.read().decode("gbk").encode("utf8")
    data = re.findall("new Array\(\"\d{2}(\d{5})\",\"(.*?)\"\)", str)
    citycode = dict([(city, code) for code, city in data])
    conn.close()


def weather(addr):
    try:
        province, city, region = re_address.match(locate(addr)).groups()
        if province in ["天津", "北京", "上海", "重庆"]:
            city = province
        if not city:
            raise

        conn = urllib.urlopen("http://weather.265.com/weather/%s.htm"
                              % citycode[city])
        str = conn.read().decode("gbk").encode("utf8")
        wdata = re.search("new Array\('(.*?)'\)", str).group(1)
        conn.close()

        report = ["%s: %s" % (t, w) for t, w 
                  in zip(["今天", "上午", "下午", "晚上",
                          "明天", "上午", "下午", "晚上"], 
                         wdata.split("','"))]
        print "%s天气: %s" % (city, ", ".join(report[:4]))
        print "%s" % ", ".join(report[4:])
    except:
        print "火星天气: 今天: -140℃~20℃, 上午: 晴, 下午: 晴, 晚上: 晴转晴"


init()

if __name__ == "__main__":
    weather(sys.argv[1])
上次由 laborer 在 2007-09-26 7:52,总共编辑 1 次。
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#18

帖子 bones7456 » 2007-09-10 9:06

:lol: :lol: :lol: 看来我那块砖也引出不少玉来了哦。

LS这个有技术含量啊,尤其是那句“火星天气: 今天: -140℃~20℃, 上午: 晴, 下午: 晴, 晚上: 晴转晴”。。。。
哈哈。。。
关注我的blog: ε==3
头像
percy
帖子: 508
注册时间: 2006-09-10 8:19
系统: Gentoo/Mac OS X
来自: Shanghai,China
联系:

#19

帖子 percy » 2008-01-06 12:29

将前面改为

代码: 全选

#!/bin/bash
#Copyright (c) 2007 bones7456 (bones7456@gmail.com)
#License: GPLv3
echo "please keyin the city "
echo -n "city:"
read city
city=`grep "$city" ./city.txt |cut -d "-" -f1`
#城市代码,留空可自动检测(自动检测不一定精确),城市代码可在 http://weather.265.com 上查询,是个5位的数字
#city=

这样直接输入城市名就可以查了。 :D
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#20

帖子 eexpress » 2008-01-06 12:36

perl的CLI参数才自动化厉害。
● 鸣学
aBiNg
帖子: 1331
注册时间: 2006-07-09 12:22
来自: 南京

#21

帖子 aBiNg » 2008-01-06 13:24

貌似这个网站报的不准哦,我在Fluxbox keys那一帖中也写了个简单的shell的,为conky。采的是yahoo的数据,locat变量是南京的;不转换encoding,直接英文了。哈哈。
上次由 aBiNg 在 2008-03-12 10:58,总共编辑 1 次。
头像
eagle5678
帖子: 1900
注册时间: 2006-07-08 14:07

#22

帖子 eagle5678 » 2008-01-06 13:34

percy 写了:将前面改为

代码: 全选

#!/bin/bash
#Copyright (c) 2007 bones7456 (bones7456@gmail.com)
#License: GPLv3
echo "please keyin the city "
echo -n "city:"
read city
city=`grep "$city" ./city.txt |cut -d "-" -f1`
#城市代码,留空可自动检测(自动检测不一定精确),城市代码可在 http://weather.265.com 上查询,是个5位的数字
#city=

这样直接输入城市名就可以查了。 :D
不知怎的,不行
头像
percy
帖子: 508
注册时间: 2006-09-10 8:19
系统: Gentoo/Mac OS X
来自: Shanghai,China
联系:

#23

帖子 percy » 2008-01-07 12:25

eagle5678 写了:
percy 写了:将前面改为

代码: 全选

#!/bin/bash
#Copyright (c) 2007 bones7456 (bones7456@gmail.com)
#License: GPLv3
echo "please keyin the city "
echo -n "city:"
read city
city=`grep "$city" ./city.txt |cut -d "-" -f1`
#城市代码,留空可自动检测(自动检测不一定精确),城市代码可在 http://weather.265.com 上查询,是个5位的数字
#city=

这样直接输入城市名就可以查了。 :D
不知怎的,不行
忘了说一下,它原来那个city.txt文件是gbk编码的,你要将它转为utf8就行了
后来又稍微修改了一下
运行结果如下

代码: 全选

 ./weather 
please keyin the city code
city:常州
**********************************************************
常州:
今天天气:
温度:5℃~14℃ 上午:晴转多云 下午:晴转多云 今晚:多云
明天天气:
温度:5℃~14℃ 上午:晴转多云 下午:晴转多云 明晚:多云
**********************************************************
附件
weather.tar.gz
(3.79 KiB) 已下载 119 次
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#24

帖子 bones7456 » 2008-01-07 12:31

nice~ :lol:
关注我的blog: ε==3
xianyuhui
帖子: 75
注册时间: 2007-11-12 20:41

#25

帖子 xianyuhui » 2008-01-14 23:13

还不错,我让它打开终端就执行
附件
Screenshot-.png
Screenshot-.png
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#26

帖子 bones7456 » 2008-01-15 11:15

xianyuhui 写了:还不错,我让它打开终端就执行
写进 .bashrc 了?呵呵,不会影响终端的打开速度吧...而且万一在没网络的环境....呵呵,不过你自己喜欢就好.
关注我的blog: ε==3
头像
iblicf
帖子: 3766
注册时间: 2007-01-15 17:15

#27

帖子 iblicf » 2008-01-15 11:37

什么时候都关心起天气来呢? :) 呵呵,
头像
solcomo
帖子: 2838
注册时间: 2007-04-25 13:12

#28

帖子 solcomo » 2008-07-11 19:48

代码: 全选

#!/bin/bash

place=`w3m -dump -no-cookie http://www.ip138.com/ips8.asp | grep 来自 | sed -e 's/.*省//g' -e 's/市.*//g' | iconv -c -f utf-8 -t gb2312`

w3m -dump -no-cookie http://php.weather.sina.com.cn/search.php?city=$place | sed -e '/^[[:space:]]*$/d' -e '/\[/d' | egrep -A 3 '今日|明日|后天'
回复