到最后,我应该有一个.txt文件的温度值为2009年的每一天。问题是,这个代码创建的文件只给了我12个值(每个月一个),其中一半来自于不真实的日期(即4月31日)。你知道吗
我对Python不是很流利,但是我已经复习了我的课本和代码好几次,没有发现任何差异。你知道吗
import urllib2
from bs4 import BeautifulSoup
#CSV
f = open('wunder-data.txt', 'w')
#months, days
for m in range(1, 13):
for d in range(1, 32):
#get if already gone through month
if (m == 2 and d > 28):
break
elif (m in [4, 6, 9, 11] and d > 30):
break
#open wunderground.com url
timestamp = '2009' + str(m) + str(d)
print "Getting data for " + timestamp
url = "http://www.wunderground.com/history/airport/KBUF/2009/" + str(m) + "/" + str(d) + "/DailyHistory.html"
page = urllib2.urlopen(url)
#get temp from page
soup = BeautifulSoup(page)
#dayTemp = soup.body.nobr.b.string
dayTemp = soup.findAll(attrs={"class":"nobr"})[4].span.string
#Format month for timestamp
if len(str(m)) < 2:
mStamp = '0' + str(m)
else:
mStamp = str(m)
#Format day for timestamp
if len(str(d)) < 2:
dStamp = '0' + str(d)
else:
dStamp = str(d)
#Build timestamp
timestamp = '2009' + mStamp + dStamp
#Write timestamp and temperature to file
f.write(timestamp + ',' + dayTemp + '\n')
# Done getting data! Close file.
f.close()
代码缩进有问题。从
#get if already..
到page = urllib2.urlopen(url)
的部分代码缩进更多,因此它只是内部循环的一部分。解析网页内容并写入文件在外循环中。这就是为什么您只抓取几个月的最后一天(事实上,由于您的循环定义为每个月31日之前,所以大部分都是无效的)。你知道吗您可以使用
datetime
正确地迭代一年中的几天,即:相关问题 更多 >
编程相关推荐