간단명료 한 python 루틴 (모 지역 의 1 년 날씨 최고 온 도 를 기어 오 르 기)

1191 단어 linuxpython
주소 http://www.wunderground.com/history/ariport/KBUF/2009/1/1/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA
#-*-coding:utf-8-*-
#!/usr/bin/python
import urllib2
from BeautifulSoup import BeautifulSoup
#      
f = open('wunder-data.txt','w')
#     
for m in range(2, 13):
	for d in range(12, 32):
	
	 #        
	 if (m == 2 and d > 28):
	  break
	 elif (m in [4, 6, 9, 11] and d > 30):
	  break
	#   url
	 timestamp = '2009'+str(m)+str(d)
	 print "Getting data for "+timestamp
	 url = "http://www.wunderground.com/history/ariport/KBUF/2009/"+str(m)+"/"+str(d)+"/DailyHistory.html"
	 page = urllib2.urlopen(url)


	#        
	 soup = BeautifulSoup(page)
	#dayTemp= soup.body.nobr.b.string
	 dayTemp = soup.findAll(attrs={"class":"nobr"})[3].span.string
	#      
	 if(len(str(m)) < 2):
	  mStamp = '0'+ str(m)
	 else:
 	  mStamp = str(m)
	
	 if(len(str(d)) < 2):
	  dStamp = '0'+ str(d)
	 else:
	  dStamp = str(d)
   	 #     
	 timestamp = '2009'+mStamp+dStamp


	 f.write(timestamp+','+dayTemp+'
') f.close()

좋은 웹페이지 즐겨찾기