gpt4 book ai didi

python - 使用 BeautifulSoup 解析 Weather Underground 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 23:48:29 25 4
gpt4 key购买 nike

我正在尝试修改代码以从 wunderground 中提取信息。然而,我尝试改编的剧本是 2008 年编写的,地下天气的格式已经改变。我在使用 soup.body.nobr.b.string 时遇到问题。我想从给定站点提取每日降雨数据。 http://www.wunderground.com/history/airport/KBUF/2011/5/2/DailyHistory.html 导入 urllib2 从 BeautifulSoup 导入 BeautifulSoup

# Create/open a file called wunder.txt (which will be a comma-delimited file)
f = open('wunder-data.txt', 'w')

# Iterate through year, month, and day
for y in range(1980, 2007):
for m in range(1, 13):
for d in range(1, 32):

# Check if leap year
if y%400 == 0:
leap = True
elif y%100 == 0:
leap = False
elif y%4 == 0:
leap = True
else:
leap = False

# Check if already gone through month
if (m == 2 and leap and d > 29):
continue
elif (m == 2 and d > 28):
continue
elif (m in [4, 6, 9, 10] and d > 30):
continue

# Open wunderground.com url
url = "http://www.wunderground.com/history/airport/KBUF/"+str(y)+ "/" + str(m) + "/" + str(d) + "/DailyHistory.html"
page = urllib2.urlopen(url)

# Get temperature from page
soup = BeautifulSoup(page)
dayTemp = soup.body.nobr.b.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(y) + mStamp + dStamp

# Write timestamp and temperature to file
f.write(timestamp + ',' + dayTemp + '\n')

# Done getting data! Close file.
f.close()

最佳答案

不要乱搞 HTML 解析,它可能会再次更改,恕不另行通知。获取one of their CSV files (HTML页面底部有链接),并用 csv 解析它模块。

关于python - 使用 BeautifulSoup 解析 Weather Underground 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7976200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com