Advertisement
jarekmor

radiation_to_file

Nov 7th, 2022
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. # radiation data from stations in Poland based on https://mapa.paa.gov.pl/
  2.  
  3. from requests_html import HTMLSession
  4. session = HTMLSession()
  5. import json
  6.  
  7. # get session and render html site
  8. r = session.get('https://mapa.paa.gov.pl/')
  9. r.html.render(sleep=1, keep_page=True, scrolldown=1)
  10.  
  11. # find all CSS 'div' Selectors
  12. results = r.html.find('div')
  13.  
  14. # list of attributes for all CSS Selectors
  15. list_attrs = []
  16. for item in results:
  17.     list_attrs.append(item.attrs)
  18.  
  19. # list of dictionares with keys and values for "miasto", "wartosc" and 'status of radiation stations
  20. radiation_list = []
  21. for item in list_attrs:
  22.     if "('station',)" in str(item):
  23.         radiation_dict = {
  24.             'miasto': item['data-nazwa'],
  25.             'wartosc': "{:.3f}".format(float(item['data-wartosc']) / 1000),
  26.             'status': item['data-status'],
  27.             }
  28.         radiation_list.append(radiation_dict)
  29.  
  30. # load data to json format
  31. jsonString = json.dumps(radiation_list, ensure_ascii=False, sort_keys=True)
  32. print(jsonString)
  33.  
  34. # write data to file
  35. with open('promieniowanie.json', 'w') as f:
  36.     json.dump(radiation_list, f, ensure_ascii=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement