question archive When prompting the user you must always include text which lets the user know what should be inputted
Subject:Computer SciencePrice:3.87 Bought7
When prompting the user you must always include text which lets the user know what should be inputted.
Update the weather.py file to do the following:
WEATHER.PY FILE:
import bs4, requests
try:
url = 'https://forecast.weather.gov/MapClick.php?'
latlong = 'lat=46.8837&lon=-102.7817'
# Combines url and zip
res = requests.get(url + latlong)
# Checks for error, and raises it as necessary
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
# Select for station
elems = soup.select('#current-conditions > div.panel-heading > div > h2')
# Retrieves station information and prints it out
stationName = elems[0].text
print(stationName)
except Exception as ex:
# Print out any error messages
print('There was a problem retrieving the information')
The following are some urls with the latitude and longitude of weather stations to test your program:
https://forecast.weather.gov/MapClick.php?lat=32.73016&lon=-117.19518
https://forecast.weather.gov/MapClick.php?lat=46.999&lon=-102.8833
https://forecast.weather.gov/MapClick.php?lat=33.417&lon=-111.8315
https://forecast.weather.gov/MapClick.php?lat=41.2607&lon=-95.9403
https://forecast.weather.gov/MapClick.php?lat=30.4391&lon=-81.703
https://forecast.weather.gov/MapClick.php?lat=41.3108&lon=-105.5903
Answer:
Python Programming:
NOTE:
Step-by-step explanation
Code:
import bs4, requests try: # website url to retrive weather station info url = "https://forecast.weather.gov/MapClick.php?" # taking input for latitude and longitude lat = input("Enter latitude of the location (Ex: 31.7798): ") lon = input("\nEnter longitude of the location (Ex: -106.3763): ") # comines lat and lon latlong = 'lat='+lat+'&lon='+lon # Combines url and zip res = requests.get(url + latlong) # Checks for error, and raises it as necessary res.raise_for_status() soup = bs4.BeautifulSoup(res.text, "html.parser") # Select for station elems = soup.select("#current-conditions > div.panel-heading > div > h2") # Retrieves station information stationName = elems[0].text # Select for current conditions elems = soup.select("#current_conditions-summary > p.myforecast-current") # Retrieves current conditions information condition = elems[0].text # Select for current temperature elems = soup.select("#current_conditions-summary > p.myforecast-current-lrg") # Retrieves current temperature in fahrenheit tempInF = elems[0].text # Select for elevation elems = soup.select("#current-conditions > div.panel-heading > div > span") # Retrieves elevation information idx = elems[0].text.find("Elev:") elevation = elems[0].text[idx + 6 : -1] # Print all retrieved information using string format method print("\nStation:{:<20}{}".format('', stationName)) print("Current Conditions:{:<9}{}".format('', condition)) print("Current Temperature:{:<8}{}".format('', tempInF)) print("Elevation:{:<18}{}".format('', elevation)) except Exception as ex: # Print out any error messages print("There was a problem retrieving the information")
Output:
PFA