使用Python编写脚本抓取黄历代码
来源:岁月联盟
时间:2010-09-24
- #!/usr/bin/python
- #coding: utf8
- import json, urllib2, time
- def get_page(url,date):
- try:
- #检查日期是否正确,例如 2010-2-31
- time.strptime(date,%Y-%m-%d)
- req = urllib2.Request(url)
- r = urllib2.urlopen(req)
- html = r.read()
- hl = json.read(html)
- if not hl[nml_Lunar_Month]:
- return;
- #print in format
- print "%s%s %s "%(hl[nml_Lunar_Month],hl[nml_Lunar_Date],hl[nml_Week]),
- print " 冲:%s<br/>宜:%s<br/>吉神:%s<br/>岁次:%s<br/>月令:%s"%(hl[nml_Chong],hl[nml_Y],hl[nml_Js],hl[nml_Sc],hl[nml_Yl])
- except Exception,e:
- #print scape
- print e
- if __name__ == __main__:
- year = 2010
- month=1
- day=1
- for month in range(1,13):
- for day in range(1,32):
- if month <10:
- month = 0 str(month)
- if day < 10:
- day = 0 str(day)
- date = str(year) - str(month) - str(day)
- get_page(http://qq.go108.com.cn/nongli/get_Nmlinfo.php?year=%s&month=%s&date=%s%(str(year),str(month),str(day)),date)