再来一个PYTHON小应用:天气查询
还是学校那个项目,终于想清楚了,打算提供个查询学校状况的飞信机器人给最终用户。
这两天玩PyFetion玩的很爽,自己对照给的例子也写了个小客户端
打算加入天气情况的定时发送。从网上找了几份代码,都不是很满意,从百度手机版找到了天气,下载下来粗看一下,行,不用正则表达式(软肋)就能解析,于是用半个多小时搞定~上次弄了个字典的,呵呵
不说废话了,贴代码:
代码
#coding:utf-8
#coder:shiweifu@gmail.com
import string
import urllib
import sys
def getWeather(to):
dic = {'from': '0',
'uid': 'frontui_1266379660_6450',
'pu': 'pd%401%2Csz%40176_208%2Cuc%400',
'word': '0312',
'ct_6': '%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2',
'bd_page_type': '1',
'tn_1': 'webmain',
'tn_6': 'weather',
'st_6': '106001',
'st_1': '111041',
'ssid': '0'}
dic["word"] = str(to)
url = 'http://m.baidu.com/s?'
url = url + urllib.urlencode(dic)
s = urllib.urlopen(url).readlines()[17]
#去除两边空格
s = s.strip()
#查找所在地区
s = s[s.find(">")+1:]
tmp = s.find("<br/>")
address = s[:tmp]
s = s[tmp+len("<br/>"):]
#今天天气
tmp = s.find("<br/>")
today = s[:tmp]
s = s[tmp+len("<br/>"):]
#明天天气
tmp = s.find("<br/>")
tomorrow = s[:tmp]
s = s[tmp+len("<br/>"):]
#获取提供
tmp = s.find("[")+1
fro = s[tmp:s.find("]")]
return {"from":fro,"tomorrow":tomorrow,"today":today,"city":address}
def main():
weather = getWeather("0314")
print weather["tomorrow"]
if __name__ == "__main__":
sys.exit(main())
#coder:shiweifu@gmail.com
import string
import urllib
import sys
def getWeather(to):
dic = {'from': '0',
'uid': 'frontui_1266379660_6450',
'pu': 'pd%401%2Csz%40176_208%2Cuc%400',
'word': '0312',
'ct_6': '%E5%A4%A9%E6%B0%94%E6%9F%A5%E8%AF%A2',
'bd_page_type': '1',
'tn_1': 'webmain',
'tn_6': 'weather',
'st_6': '106001',
'st_1': '111041',
'ssid': '0'}
dic["word"] = str(to)
url = 'http://m.baidu.com/s?'
url = url + urllib.urlencode(dic)
s = urllib.urlopen(url).readlines()[17]
#去除两边空格
s = s.strip()
#查找所在地区
s = s[s.find(">")+1:]
tmp = s.find("<br/>")
address = s[:tmp]
s = s[tmp+len("<br/>"):]
#今天天气
tmp = s.find("<br/>")
today = s[:tmp]
s = s[tmp+len("<br/>"):]
#明天天气
tmp = s.find("<br/>")
tomorrow = s[:tmp]
s = s[tmp+len("<br/>"):]
#获取提供
tmp = s.find("[")+1
fro = s[tmp:s.find("]")]
return {"from":fro,"tomorrow":tomorrow,"today":today,"city":address}
def main():
weather = getWeather("0314")
print weather["tomorrow"]
if __name__ == "__main__":
sys.exit(main())