Advertisement
Typhoon

Get Temperature Data - Bratislava

Aug 24th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Install BS4 with pip3 : sudo pip3 install -U beautifulsoup4
  5. from bs4 import BeautifulSoup
  6. # Install Requests with pip3 : sudo pip3 install -U requests
  7. import urllib.request as urllib2
  8.  
  9. # Define URL for data scraping
  10. checkurl= 'http://pocasie.sme.sk/krajina/slovensko/bratislava/'
  11. # Open URL in URLlib
  12. checkpage = urllib2.urlopen(checkurl)
  13. # Read and parse html data
  14. checksoup = BeautifulSoup(checkpage.read(), "html.parser", from_encoding="UTF-8")
  15.  
  16. # Get values with BS
  17. temp_current_val = checksoup.find_all('span', 'pocasie-current-box-now-temp')[0].text.split('°')[0]
  18. temp_today_max = checksoup.find_all('span', 'pocasie-current-box-minmax-max')[0].text.split()[2]
  19. temp_today_min = checksoup.find_all('span', 'pocasie-current-box-minmax-min')[0].text.split()[2]
  20.  
  21. # Print values
  22. print ("Aktualna Teplota: ", temp_current_val)
  23. print ("Dnesne Maximum: ", temp_today_max)
  24. print ("Dnesne Minimum: ", temp_today_min)
  25. # Another method how to parse data
  26. temp_today_alternative = checksoup.find_all('b')[1].text.replace('°C','')
  27. print ("\nAlternative temp: " , temp_today_alternative)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement