私は、空港コードと年をユーザーに尋ねるプログラムを手に入れようとしています(何でも構いません - それは正しいものでも特定のものでもなくても構いません。データを印刷する) は、ここで私が持っているコードです:とにかくPython Wunderground raw_inputデータ
import urllib2
from bs4 import BeautifulSoup
# Create/open a file called wunderdata.txt which will be a CSVfile
f = open('wunderdata.txt', 'w')
# Iterate through months and day
for m in range(1, 13):
for d in range(1,32):
# Check if already processed all days in the month
if (m == 2 and d> 28):
break
elif (m in[4, 6, 9, 11] and d > 30):
break
# Open wunderground.com url
airport = str(raw_input("Enter airport code: "))
year = str(raw_input("Enter year: "))
timestamp = '2009' + str(m) + str(d)
print ("Getting data for ") + timestamp
url = "http://www.wunderground.com/history/airport/" + airport + "/" + year + "/" + str(m) + "/" + str(d) + "/DailyHistory.html?"
page = urllib2.urlopen(url)
# Get temperature from page
soup = BeautifulSoup(page, "html.parser")
#the following two lines are the original (textbook) and first attempt to fix
# dayTemp = soup.body.wx-value.b.string
dayTemp = soup.findAll(attrs={"class":"wx-value"})[6].get_text()
seaLevel = soup.findAll(attrs={"class":"wx-value"})[16].get_text()
# 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 + " " + "Sea Level Pressure: " + seaLevel + '\n')
# Done getting data! Close file.
f.close()
、これは、入力されたときに来るものです:
python get-weather-data.py
Enter airport code: KBUF
Enter year: 2009
Getting data for 200911
Enter airport code: KBUF
Enter year: 2009
Getting data for 200912
Enter airport code: KBUF
Enter year: 2009
Getting data for 200913
をそして、私はそれになりたい
python get-weather-data.py
Enter airport code: KBUF
Enter year: 2009
Getting data for 200911
Getting data for 200912
Getting data for 200913
誰かが助けてください!私は初心者ですので、私は多くのPythonについて知らないが、非常に助けを感謝します:)