Close

Adventures with Python

A project log for Web powered antique wind gauge

An antique volt meter paired with a Raspberry Pi to display real time wind data.

mechanicalsquidmechanicalsquid 11/26/2014 at 15:520 Comments

I'm not a programmer. I can stumble through C and MATLAB, but it's never elegant.

While I could do this in C, from what I've heard about Python and web scraping with Beautiful Soup, it could make this project a bit easier. So I set out to write a simple script to just grab the webpage and display the wind speed on the command line.

So armed with web tutorials on Python and Beautiful Soup, I attempted to grab the webpage.

This turned out to be harder than I thought. Firstly, the site in question www.bramblemet.co.uk displays the windspeed as an image. Not helpful. I did have the thought though that it appears that the site hasn't been updated since the mid 90's. I tried on a whim to see if there was a WAP version of the site. I was rewarded by finding: www.bramblemet.co.uk/wap

However. Firstly, there's a re-direct with a random string generated per session inserted in the URL. Secondly, the information I need is on the second page. Because of the random string inserted in the URL, I can't go straight there. Attempts to do so just redirect to the first page.

So, I need to access the site to get the string in the URL, and then follow the link to page 2. Once I'd got the page, it can be passed into Beautiful Soup and then the pertinent number grabbed.

I'm really pleased I used Python for this, as despite being a complete beginner in the language, even with these redirects and links, within an hour I had a working solution.

Here's my v 0.01 code:

#Grab windspeed from bramble met, display on the command line
#mechanicalsquid 2014

from bs4 import BeautifulSoup
import requests

r=requests.get("http://www.bramblemet.co.uk/wap/") #Get the first page

data=r.text
soup=BeautifulSoup(data) #Pass to beautiful soup
link=soup.find('a') #Extract links

p=requests.get(r.url[:-18]+link.get('href')) #Get second page, including random string in URL from page 1
windpage=p.text
soup=BeautifulSoup(windpage) #Pass to beautiful soup
windspeed=int(soup.prettify()[309:315]) #Grab the numbers

print("The current windspeed in the Solent is: "+ str(windspeed)+" Knots")

Discussions