Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Name = Erik V.
- # Student number = 221122
- import xml.etree.ElementTree as ET
- import urllib.request
- from time import sleep
- from threading import Thread
- count = 0
- def getXmlTree(url):
- """Will return the XML tree made with the url given"""
- # Getting the data from the url
- with urllib.request.urlopen(url) as url:
- data = url.read()
- # Making a tree with the data
- tree = ET.fromstring(data)
- return tree
- def getXmlInfoInDic():
- """Will return the info at the id in a dictionary"""
- # Leeuwarden = 6270 Groningen 6280
- # Getting the XML tree from the url
- tree = getXmlTree("https://data.buienradar.nl/1.0/feed/xml")
- # Getting the info for the current ID (leeuwarden)
- node = tree.findall('''.//*[@id='6270']''')[0]
- # Making a dictionary to store the data in
- weatherDic = {}
- for element in node.iter():
- weatherDic.update({element.tag : element.text})
- return weatherDic
- def saveData(weatherDic):
- """Will save the data from the dictionary to the file"""
- global count
- filename = "weatherDataCollection.txt"
- file = open(filename, "a+")
- for key, value in weatherDic.items():
- file.write("{} : {}\n".format(key, value))
- file.write("\n")
- count += 1
- print("Weather data collection number {} completed".format(count))
- def main():
- # Saving the data every hour
- while True:
- # Getting a dictionary filled with the weather data
- weatherDic = getXmlInfoInDic()
- # Saving the data to the file
- saveData(weatherDic)
- # Letting the program sleep for an hour (3600s) before repeating
- sleep(1800)
- if (__name__ == "__main__"):
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement