我正在使用以下代码(根据 Nathan Yau 的“Visualize This”早期示例稍作修改)从 WUnderGround 的网站上抓取天气数据。如您所见,python 正在从类名为“wx-data”的元素中获取数值数据。
但是,我还想从 DailyHistory.htmml 中获取平均湿度。 问题是并非所有的“跨度”元素都有一个类名,平均湿度单元就是这种情况。如何使用 BeautifulSoup 和下面的代码选择这个特定的单元格?
(这里是一个被抓取页面的例子——点击你的开发模式并搜索'wx-data'来查看被引用的'span'元素:
http://www.wunderground.com/history/airport/LAX/2002/1/1/DailyHistory.html)
import urllib2
from BeautifulSoup import BeautifulSoup
year = 2004
#create comma-delim file
f = open(str(year) + '_LAXwunder_data.txt','w')
#iterate through month and day
for m in range(1,13):
for d in range (1,32):
#Chk if already gone through month
if (m == 2 and d > 28):
break
elif (m in [4,6,9,11]) and d > 30:
break
# open wug url
timestamp = str(year)+'0'+str(m)+'0'+str(d)
print 'Getting data for ' + timestamp
url = 'http://www.wunderground.com/history/airport/LAX/'+str(year) + '/' + str(m) + '/' + str(d) + '/DailyHistory.html'
page = urllib2.urlopen(url)
#Get temp from page
soup = BeautifulSoup(page)
#dayTemp = soup.body.wx-data.b.string
dayTemp = soup.findAll(attrs = {'class':'wx-data'})[5].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 = str(year)+ mStamp + dStamp
#Wrtie timestamp and temp to file
f.write(timestamp + ',' + dayTemp +'\n')
#done - close
f.close()