Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import urllib request
- import urllib.request
- #import urllib error handling
- '''
- ## create dictionary to hold 4 cities
- cityChoices = {"Annapolis":"KNAK","Boca Raton":"KBCT","Boston":"KBOS","Napa":"KAPC"}
- key = cityChoices(name)
- '''
- '''
- First, download the file using urllib.request and then save the file.
- Name file the city name + .xml
- open file using bs4, parse and print out info, as req'd
- '''
- #Some Variables to initiate
- #link = ""
- #url = 'https://w1.weather.gov/xml/current_obs/' +link, '.xml'
- #url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=' +link
- decoded = ''
- print('Your Weather Report')
- print()
- print('Current observations are available for: ')
- print("Annapolis | Boca Raton | Boston | Napa | KAPC")
- '''
- ## print all 4 city names
- for x in cityChoices.keys():
- print("-", x)
- print (cityChoices)
- print (cityChoices.keys())
- '''
- ## let's get the user input and run it against our available cities
- user_input = input('Enter the city you would like a weather report for: ')
- '''
- if user_input in cityChoices.keys():
- match = data(user_input)
- print(match)
- else:
- while user_input not in cityChoices.keys():
- print("No data available. \n")
- user_input = input("Please try another city: ")
- '''
- link = user_input
- if user_input == "Napa":
- url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KAPC'
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- elif user_input == "Annapolis":
- url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KNAK'
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- elif user_input == "Boca Raton":
- url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KBCT'
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- elif user_input == "Boston":
- url = 'https://w1.weather.gov/xml/current_obs/display.php?stid=KBOS'
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- else:
- user_input == input("No data available.. Please try another city: ")
- '''
- if user_input == "Napa":
- link = "KAPC"
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- elif user_input == "Annapolis":
- link == "KNAK"
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- elif user_input == "Boca Raton":
- link == "KBCT"
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- elif user_input == "Boston":
- link == "KBOS"
- print('Accessing weather data . . . The current weather has been accessed for ' +user_input)
- else:
- user_input == input("No data available.. Please try another city: ")
- '''
- #function that downloads the file
- # For text files, or other ascii types, just pass an empty string
- # for the second param aka Stuff(file_name,'',url)
- def downloadFile(file_name,file_mode,url):
- i=0
- while i < 1:
- with urllib.request.urlopen(url) as f:
- s = f.read().decode('utf-8')
- #print("downloading ", url)
- print(s)
- i += 1
- break
- # Open our local file for writing
- local_file = open(file_name, "w" + file_mode)
- #Write to our local file
- local_file.write(s)
- local_file.close()
- #create file name based on known pattern
- file_name = user_input + ".xml"
- downloadFile(file_name,'',url)
- print(file_name, +' created successfully')
- reporting_data = file_name
- l = user_input
- e = ''
- u = url
- downloadFile(l,e,u)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement