分页: 1 / 2

一个天气预报的脚本

发表于 : 2007-09-04 14:18
bones7456
闲来无事,写了天气预报的脚本,与大家分享。
本人菜鸟,如有任何问题或建议,欢迎指正。谢谢。

数据来自 http://weather.265.com

20080524更新,感谢bthink发现265的改版消息。

发表于 : 2007-09-04 14:31
vim7
tar.. - -!!
用什么写的? shell?

发表于 : 2007-09-04 15:24
ofewiofewo
呵呵,学习,填了几个city参数

广州: 25℃~29℃ 下午:阴转多云,阵雨;晚上:阴转多云,小雨
漳州: 24℃~29℃ 下午:阴转多云,小阵雨;晚上:阴转多云,有雨
丹东: 17℃~28℃ 下午:晴转多云;晚上:晴转多云

发表于 : 2007-09-04 16:03
bones7456
vim7 写了:tar.. - -!!
用什么写的? shell?
就是shell。
ofewiofewo 写了:呵呵,学习,填了几个city参数

广州: 25℃~29℃ 下午:阴转多云,阵雨;晚上:阴转多云,小雨
漳州: 24℃~29℃ 下午:阴转多云,小阵雨;晚上:阴转多云,有雨
丹东: 17℃~28℃ 下午:晴转多云;晚上:晴转多云
:D :D :D 见笑了。

发表于 : 2007-09-04 16:34
zhan
无聊的人又多了一个.

http://rafb.net/p/APZfJp42.html

代码: 全选

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib, re, sys

def getcitycode():
        conn=urllib.urlopen("http://weather.265.com/get_weather.php?action=get_city")
        citysrc=conn.read().decode("gbk").encode("utf8")
        conn.close()
        citycode=re.findall('[0-9]{5}', citysrc)[0]
        return citycode

def getweather(code):
        url="http://weather.265.com/weather/"+code+".htm"
        conn=urllib.urlopen(url)
        weasrc=conn.read().decode("gbk").encode("utf8")
        conn.close()
        wea=re.findall('show_weather\("(.*)"\)', weasrc)[0].split("'")
        weather=[item  for item in wea if item !="," and ")" not in item and "(" not in item]
        city=wea[0].split('"')[0]
        option=[u'今天:',u'上午:',u'下午:',u'晚上:',u'明天:',u'上午:',u'下午:',u'晚上:']
        #print "\n".join(weather)
        index=0
        print u'城市:',city
        while index < (len(option)):
                print option[index],weather[index]
                index=index+1

if __name__=='__main__':
        try:
                code=sys.argv[1]
        except:
                code=getcitycode()
        getweather(code)
python 版的,比 bash 的好看点...

发表于 : 2007-09-04 16:43
eexpress
py的库,比shell的多啊。 :lol:

发表于 : 2007-09-04 19:24
kyowu
是这样用么?
解压后,
chmod -x weather
./weather

为什么提示
bash: ./weather:Permission denied
???

发表于 : 2007-09-04 19:45
BigSnake.NET
kyowu 写了:是这样用么?
解压后,
chmod -x weather
./weather

为什么提示
bash: ./weather:Permission denied
???

代码: 全选

chmod u+x weather

发表于 : 2007-09-04 22:42
gregorian
chmod +x

建议多看看基础的命令,再来学shell

发表于 : 2007-09-05 13:10
fireshort
用起来挺方便,不过265的数据是不是不太准确,好像跟121的相差比较大?

发表于 : 2007-09-05 15:28
kyowu
grep 'wid_265=' | sed -e 's/document\.cookie\ =\ "wid_265=//' | sed -e 's/".*//g'

这句话如何解析呀?

发表于 : 2007-09-05 16:26
bones7456
kyowu 写了:grep 'wid_265=' | sed -e 's/document\.cookie\ =\ "wid_265=//' | sed -e 's/".*//g'

这句话如何解析呀?
在文本中挑出有含有'wid_265='的那行,再剔除
document.cookie = "wid_265=

"后面的内容。
最终就只留下个数字了。

发表于 : 2007-09-05 16:28
bones7456
kyowu 写了:是这样用么?
解压后,
chmod -x weather
./weather

为什么提示
bash: ./weather:Permission denied
???
:shock: :shock: :shock: tar包解开了应该本来就有可执行权限吧,偏偏要把它给去掉~~~

发表于 : 2007-09-05 16:49
kyowu
明白了

发表于 : 2007-09-08 17:56
Beetle
多个城市代码是用什么隔开?