Python Forum
Displaying selected information from XML file in the Spyder console(weather forecast)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying selected information from XML file in the Spyder console(weather forecast)
#1
On the site Yr (www.yr.no) you can download the weather forecast in XML format. For example, we can download the hourly forecast using the address in the form:

https:// www. yr. no/ place/ Country/ Region/ Place/ forecast_hour_by_hour.xml.

For example, for the city of Boston, in United States, Massachusetts, it will be:

https://www.yr.no/place/United_States/Ma...y_hour.xml

I would like to write a python script that, based on an XML file, for example:

https://www.yr.no/place/United_States/Ma...y_hour.xml,

displays the following data:

- place name,

- longitude, latitude and altitude,

- Forecasts for cloudiness / precipitation (for example "Partly cloudy" or "Light rain") and temperature for each hour.

I would like the program in the console to ask the user about the path to the xml file, and then the program should display the forecast in the form I wrote earlier.

I don't care if the console in Spyder will display information from the downloaded .xml file to the disk, for example: C:\Users\John_pl\Downloads\forecast_hour_by_hour_Boston.xml,
or directly from the website: https://www.yr.no/place/United_States/Ma...y_hour.xml

At the moment I went to the Yr website and downloaded the xml file using the ctrl + s keyboard shortcut. This file can be downloaded here: https://www97.zippyshare.com/v/2ltYcl3R/file.html

When you download this file and write a python script in Spyder specifically for this file, the console (output) in Spyder is to look exactly like this:

Output:
Location: Boston Longitude: -71.05977 Latitude: 42.35843 Altitude: 14 From 2020-06-19 04:00:00 to: 2020-06-19 05:00:00 Cloudiness/precipitation: Cloudy, Temperature: 20 From 2020-06-19 05:00:00 to: 2020-06-19 06:00:00 Cloudiness/precipitation: Cloudy, Temperature: 20 From 2020-06-19 06:00:00 to: 2020-06-19 07:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 20 From 2020-06-19 07:00:00 to: 2020-06-19 08:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 21 From 2020-06-19 08:00:00 to: 2020-06-19 09:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 23 From 2020-06-19 09:00:00 to: 2020-06-19 10:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 25 From 2020-06-19 10:00:00 to: 2020-06-19 11:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 26 From 2020-06-19 11:00:00 to: 2020-06-19 12:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 28 From 2020-06-19 12:00:00 to: 2020-06-19 13:00:00 Cloudiness/precipitation: Clear sky, Temperature: 29 From 2020-06-19 13:00:00 to: 2020-06-19 14:00:00 Cloudiness/precipitation: Clear sky, Temperature: 30 From 2020-06-19 14:00:00 to: 2020-06-19 15:00:00 Cloudiness/precipitation: Clear sky, Temperature: 31 From 2020-06-19 15:00:00 to: 2020-06-19 16:00:00 Cloudiness/precipitation: Clear sky, Temperature: 31 From 2020-06-19 16:00:00 to: 2020-06-19 17:00:00 Cloudiness/precipitation: Clear sky, Temperature: 31 From 2020-06-19 17:00:00 to: 2020-06-19 18:00:00 Cloudiness/precipitation: Clear sky, Temperature: 31 From 2020-06-19 18:00:00 to: 2020-06-19 19:00:00 Cloudiness/precipitation: Clear sky, Temperature: 30 From 2020-06-19 19:00:00 to: 2020-06-19 20:00:00 Cloudiness/precipitation: Clear sky, Temperature: 27 From 2020-06-19 20:00:00 to: 2020-06-19 21:00:00 Cloudiness/precipitation: Clear sky, Temperature: 25 From 2020-06-19 21:00:00 to: 2020-06-19 22:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 24 From 2020-06-19 22:00:00 to: 2020-06-19 23:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 23 From 2020-06-19 23:00:00 to: 2020-06-20 00:00:00 Cloudiness/precipitation: Fair, Temperature: 22 From 2020-06-20 00:00:00 to: 2020-06-20 01:00:00 Cloudiness/precipitation: Fair, Temperature: 21 From 2020-06-20 01:00:00 to: 2020-06-20 02:00:00 Cloudiness/precipitation: Fair, Temperature: 21 From 2020-06-20 02:00:00 to: 2020-06-20 03:00:00 Cloudiness/precipitation: Fair, Temperature: 21 From 2020-06-20 03:00:00 to: 2020-06-20 04:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 20 From 2020-06-20 04:00:00 to: 2020-06-20 05:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 20 From 2020-06-20 05:00:00 to: 2020-06-20 06:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 20 From 2020-06-20 06:00:00 to: 2020-06-20 07:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 20 From 2020-06-20 07:00:00 to: 2020-06-20 08:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 22 From 2020-06-20 08:00:00 to: 2020-06-20 09:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 24 From 2020-06-20 09:00:00 to: 2020-06-20 10:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 26 From 2020-06-20 10:00:00 to: 2020-06-20 11:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 29 From 2020-06-20 11:00:00 to: 2020-06-20 12:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 30 From 2020-06-20 12:00:00 to: 2020-06-20 13:00:00 Cloudiness/precipitation: Fair, Temperature: 32 From 2020-06-20 13:00:00 to: 2020-06-20 14:00:00 Cloudiness/precipitation: Clear sky, Temperature: 33 From 2020-06-20 14:00:00 to: 2020-06-20 15:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 33 From 2020-06-20 15:00:00 to: 2020-06-20 16:00:00 Cloudiness/precipitation: Fair, Temperature: 32 From 2020-06-20 16:00:00 to: 2020-06-20 17:00:00 Cloudiness/precipitation: Fair, Temperature: 33 From 2020-06-20 17:00:00 to: 2020-06-20 18:00:00 Cloudiness/precipitation: Fair, Temperature: 32 From 2020-06-20 18:00:00 to: 2020-06-20 19:00:00 Cloudiness/precipitation: Fair, Temperature: 31 From 2020-06-20 19:00:00 to: 2020-06-20 20:00:00 Cloudiness/precipitation: Clear sky, Temperature: 30 From 2020-06-20 20:00:00 to: 2020-06-20 21:00:00 Cloudiness/precipitation: Fair, Temperature: 28 From 2020-06-20 21:00:00 to: 2020-06-20 22:00:00 Cloudiness/precipitation: Clear sky, Temperature: 26 From 2020-06-20 22:00:00 to: 2020-06-20 23:00:00 Cloudiness/precipitation: Clear sky, Temperature: 25 From 2020-06-20 23:00:00 to: 2020-06-21 00:00:00 Cloudiness/precipitation: Fair, Temperature: 24 From 2020-06-21 00:00:00 to: 2020-06-21 01:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 23 From 2020-06-21 01:00:00 to: 2020-06-21 02:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 22 From 2020-06-21 02:00:00 to: 2020-06-21 03:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 21 From 2020-06-21 03:00:00 to: 2020-06-21 04:00:00 Cloudiness/precipitation: Partly cloudy, Temperature: 21
I am a meteorologist (not a programmer). In my daily work, I open a raw xml file and manually convert the data from the XML file to the appearance as in the "Output" window above. So I'm just copying the information I need - Location, Longitude, Latitude, Altitude, Hourly Forecast (From, to, Cloudiness/precipitation, Temperature).

I need a script in which, after providing a link to the xml file from the Yr page, or entering the path to the xml file on the disk, the program will display information in the console in the form as in the "Output" window above.

If you helped me write a script in Spyder, I would save a lot of time in my work. I would write this month myself, maybe it will be a matter of minutes for you. Please help!
Reply
#2
(Jun-19-2020, 03:03 PM)JohnN_pl Wrote: maybe it will be a matter of minutes for you. Please help!
Can use a couple minutes to help you get started.
import requests
from bs4 import BeautifulSoup

url = 'https://www.yr.no/place/United_States/Massachusetts/Boston/forecast_hour_by_hour.xml'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'xml')
location = soup.find('location', altitude="14")
Usage test:
>>> location.get('geobaseid')
'4930956'
>>> location.get('latitude')
'42.35843'
>>> location.get('longitude')
'-71.05977'
>>> 
>>> # Forcast using spesific date eg find_all('time') get all
>>> forcast = soup.find('time', {'from': '2020-06-19T15:00:00', 'to': '2020-06-19T16:00:00'})
>>> forcast
<time from="2020-06-19T15:00:00" to="2020-06-19T16:00:00">
<!-- Valid from 2020-06-19T15:00:00 to 2020-06-19T16:00:00 -->
<symbol name="Clear sky" number="1" numberEx="1" var="01d"/>
<precipitation value="0"/>
<!-- Valid at 2020-06-19T15:00:00 -->
<windDirection code="SSW" deg="210.1" name="South-southwest"/>
<windSpeed mps="4.8" name="Gentle breeze"/>
<temperature unit="celsius" value="31"/>
<pressure unit="hPa" value="1016.4"/>
</time>
>>> 
>>> forcast.find('symbol')
<symbol name="Clear sky" number="1" numberEx="1" var="01d"/>
>>> forcast.find('symbol').get('name')
'Clear sky'
>>> 
>>> forcast.find('temperature')
<temperature unit="celsius" value="31"/>
>>> forcast.find('temperature').get('value')
'31'
Reply
#3
I haven't had contact with python since my student days, I work as a meteorologist. I will go to the python course, but could you write this script for me once? I will perform my duties at work much faster. I would be very grateful to you, please help me
Reply
#4
this looks very much like homework, so I moved it to relevant section
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Will you let me edit the main subject text? I will write it shorter and more clearly, and paste the code I have already written
Reply
#6
(Jun-20-2020, 11:28 AM)JohnN_pl Wrote: Will you let me edit the main subject text? I will write it shorter and more clearly, and paste the code I have already written
Just make a new post here with changes that you have done and code written.
Reply
#7
I will do a completely new topic, I'm halfway to completing the code, and in the next topic I will specify exactly where I got stuck. In the current topic, when most people see my main post at the top, they get discouraged and don't read further. Delete the current topic, I will make a new one. In the new one I will describe in two sentences where I am stuck and show my code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  One more issue with displaying API information card51shor 15 4,686 Jun-25-2020, 07:10 AM
Last Post: card51shor
  Applied Data Science with Python - homework 2.2 (Weather plotting) eyavuz21 4 3,413 Jun-03-2020, 07:09 PM
Last Post: eyavuz21
  Trouble with "Weather Program" Assignment sarah_mb_sues 5 9,073 Aug-10-2018, 02:29 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020