Beetle 写了:多个城市代码是用什么隔开?




代码: 全选
$ python weather.py forum.ubuntu.org.cn
东莞天气: 今天: 26℃~33℃, 上午: 多云, 下午: 阴转多云,雷雨, 晚上: 阴转多云,阵雨
明天: 26℃~31℃, 上午: 阴转多云,小阵雨, 下午: 阴转多云,雷雨, 晚上: 阴转多云,雷雨
代码: 全选
# 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])
代码: 全选
#!/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=
不知怎的,不行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=
忘了说一下,它原来那个city.txt文件是gbk编码的,你要将它转为utf8就行了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=
代码: 全选
./weather
please keyin the city code
city:常州
**********************************************************
常州:
今天天气:
温度:5℃~14℃ 上午:晴转多云 下午:晴转多云 今晚:多云
明天天气:
温度:5℃~14℃ 上午:晴转多云 下午:晴转多云 明晚:多云
**********************************************************
代码: 全选
#!/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 '今日|明日|后天'