以前一直使用中国天气网的天气预报组件都挺好,可是自从他们升级组件后数据加载变得非常不稳定,因为JS的阻塞常常导致网站打开速度很慢。为了解决这个问题决定现学现用python编写一个抓取程序,每天定时抓取最新的天气情况并生成静态JS供网站调用。由于初学python,程序有些地方写得不是很优雅,还望高手指正。
代码如下:
#!/usr/bin/env python
#coding:UTF-8
import urllib,os,datetime
def GetWeather(cityid):
"获取指定城市的天气情况"
#http://www.weather.com.cn/data/cityinfo/101110301.html
#{"weatherinfo":{"city":"延 长","cityid":"101110301","temp1":"31℃","temp2":"18℃","weather":"多 云","img1":"d1.gif","img2":"n1.gif","ptime":"08:00"}}
url="http://www.weather.com.cn/data/cityinfo/"+cityid+".html"
Result=""
try:
web=urllib.urlopen(url)
content=web.read().decode('utf-8').replace(&#