首页 > 程序开发 > Web开发 > Python >

使用Python编写脚本抓取黄历代码

2010-09-24

Python代码 #!/usr/bin/python   #coding: utf8   import json, urllib2, time     def get_page(url,date):       try: &nbs

Python代码
  1. #!/usr/bin/python
  2. #coding: utf8
  3. import json, urllib2, time
  4. def get_page(url,date):
  5. try:
  6. #检查日期是否正确,例如 2010-2-31
  7. time.strptime(date,%Y-%m-%d)
  8. req = urllib2.Request(url)
  9. r = urllib2.urlopen(req)
  10. html = r.read()
  11. hl = json.read(html)
  12. if not hl[nml_Lunar_Month]:
  13. return;
  14. #print in format
  15. print "%s%s %s "%(hl[nml_Lunar_Month],hl[nml_Lunar_Date],hl[nml_Week]),
  16. 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])
  17. except Exception,e:
  18. #print scape
  19. print e
  20. if __name__ == __main__:
  21. year = 2010
  22. month=1
  23. day=1
  24. for month in range(1,13):
  25. for day in range(1,32):
  26. if month <10:
  27. month = 0 str(month)
  28. if day < 10:
  29. day = 0 str(day)
  30. date = str(year) - str(month) - str(day)
  31. get_page(http://qq.go108.com.cn/nongli/get_Nmlinfo.php?year=%s&month=%s&date=%s%(str(year),str(month),str(day)),date)
相关文章
最新文章
热点推荐