Pollen Check V1 (Python)

My beautiful and amazing wife has challenged me with creating a Python program which checks the pollen count each morning and e-mails it to her. I’ve broken this task up into three parts:

  • 1st part of Python program that gets the pollen count
  • 2nd part of Python program handles the e-mail
  • 3rd part will setting Cron to run the Python program

So the first part of the Python program is getting the pollen count data. I found a few APIs that offered said data, but they wanted signing up to. I checked and my wife normally reads the pollen information on the BBC Weather website. Since she already gets the data from there why not use that as the data source for my program…

pollen_python_1
The Pollen data – currently showing as VH

After finding the data on the website it is time to slip into designer view and finding the relevent tags that hold the data.

pollen_python_2.png

With this information in hand, I fired up PyCharm and got to work on my Python program. Using the knowledge I gained writing my Amazon Price Check program, I instantly fell back to BeautifulSoup and got that to the do the hard work.


#!/bin/python3
#geektechstuff

#libraries to import
from datetime import datetime
import bs4
import requests

current_time = datetime.now()

#bbc website for Salford Weather
url = 'https://www.bbc.co.uk/weather/2638671'

#headers to identify the program as a web browser
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6'}

#get the website
res = requests.get(url,headers = headers)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')

pollen_level_ue = soup.findAll('span', {'class': 'wr-c-environmental-data__full-text wr-hide-visually'})
last_update_time = soup.findAll('time')



#Edit the pollen text to be more readable
ttedit=str(pollen_level_ue)
head,sep,tail=ttedit.partition('>')
ttedit2=tail
head,sep,tail=ttedit2.partition('<')
pollen_level_edited=head.strip()

#edit time text to be more readable
time_edit=str(last_update_time)
head2,sep2,tail2=time_edit.partition('>')
time_edit2=tail2
head2,sep2,tail2=time_edit2.partition('<')
time_edited=head2.strip()

current_time_edit=str(current_time)
head3,sep3,tail3=current_time_edit.partition('.')
current_time_edited=head3

print('-------------------')
print('Pollen Level for Salford')
print('-------------------')
print('')
print('Pollen level is:', pollen_level_edited)
print('')
print('Last updated at:', time_edited)
print('It is currently', current_time_edited)
print('')
print('Data from BBC Weather website')

pollen_python_3.png

So far, success. Now just need to run it a few times (at different times) and make sure the updated time / pollen information keeps up to date with the website, then its e-mail time 🙂

2 thoughts on “Pollen Check V1 (Python)

Comments are closed.