Back to Posts

Connecting a Python script to IFTTT

Posted in datascience

Once you start using IFTTT there’s no going back. For many things it’s just way too comfortable and does not require any sort of programming skills to get stuff done.

So assuming you know a little bit about it, here’s a method to connect your Python (or whatever) script to a Maker Channel on IFTTT. The aim? Building a small web scraper that sends a notification to your phone with the scraped data.

This recipe is based on this blog post (which is really well done), that teaches you how to setup a Maker Channel on IFTTT to trigger with your script, you should check out (at least) the first part of that post in order to create your Maker Channel.

There are just two components here: a Python scraper and the Channel to trigger. If you’ve got the channel ready, here’s how to build the script.

What do we want it to do?

  • Make a request to a website
  • Get the data (and maybe work on it)
  • Send that data to the Maker Channel

Here is the full code, let’s go through it:

import requests  
from bs4 import BeautifulSoup

url = ‘http://www.enricobergamini.it/trialpage.html'

#load and scrape  
response = requests.get(url)   
html = response.content  
soup = BeautifulSoup(html, “lxml”)  
number = soup.find(‘h1’, id=’number’).text

#notify  
def notification(message):  
 report = {}  
 report[“value1”] = message  
 requests.post(“https://maker.ifttt.com/trigger/NAME_OF_CHANNEL/with/key/ID_OF_THE_KEY", data=report)  

notification(number)

First we’ll import two (amazingly good) Python libraries: BeautifulSoup and Requests.

I’ve set up a sample page to scrape, here. That we set as ‘url’ variable. The scraping is really basic, we are just loading the page with a GET request, and looking for the ‘h1’ tag with a “number” id in its HTML code.

The notification method is exactly the same explained in the blog post linked above, with just one “value”.

I’ve set my IFTTT recipe to send a notification to my phone when the Channel is triggered, but you can really do whatever with IFTTT.

Why is this useful?

The cool thing is that you can do a thousand things with this kinda game, since you can scrape data somewhere and use it as a IFTTT variable!

You can, for example, put the script in Cron and make it check something for you everyday or a few times a day. Maybe there’s something with a moving price that you wanna buy online but you bother check often. You could also put something like (‘if this number is lower then … notify me’) in your script so you’ll be the first one knowing it.

Or maybe you wanna scrape and tweet, or scrape and store somewhere, or scrape and IOT.

This post just explains the link between the two techniques, the number of applications are enormous :)

The internet is the most incredible infrastracture ever built. Human beings learn through dialogues. Open sourcing your ideas means enhancing them. I keep my brain alive reading, writing and coding. Economics student, I'm (slowly) teaching myself Data Science. Hacktivism with www.onData.it folks.

Read Next

Usare VirtualENV in Python

ActionAid

fights for food, women, a right governance

GET INVOLVED