Get a notification when questions get posted I like to help people on StackOverflow, matter of fact nowadays it depends on how quick you are when answering question.
With that being said, I can't always keep a tab and an eye to refresh the browser to view new questions.

I made a very basic script to send me a notification (OSX) whenever there's a new question.

import requests, os, threading, time
from bs4 import BeautifulSoup
from timeloop import Timeloop
from datetime import timedelta

tl = Timeloop()

SE_REALTIME = "https://stackexchange.com/questions?tab=realtime"
WATCHED_TAG = "swift"
QUE_LI = []



# The notifier function
def notify(title, subtitle, message, link):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    l = '-open {!r}'.format(link)
    os.system('terminal-notifier {}'.format(' '.join([m, t, s, l])))

def fetch():
    # The SOUP
    soup = BeautifulSoup(requests.get(SE_REALTIME).content, "html.parser")

    # List of Questions with the sid: Stackoverflow.com
    li = soup.find_all('div', {'data-sid': 'stackoverflow.com'})

    # Loop Through
    for child in li:
        id = child['class'][2]
        all_tags = child.find("span", attrs={'class': 'realtime-tags'}).text.strip()
        tags = all_tags.split()
        if WATCHED_TAG in tags:
            if id not in QUE_LI: 
                question = child.find("a", attrs={'class': 'realtime-question-url realtime-title'})
                QUE_LI.append(id)
                notify(title = 'New Question {}'.format(' '.join([WATCHED_TAG])),
                subtitle = 'Stackoverflow.com',
                message  = question.text.strip(),
                link = question['href'])



@tl.job(interval=timedelta(seconds=2))
def fetch_questions_every_2s():
    fetch()

if __name__ == "__main__":
    tl.start(block=True)


Ok, This code works well. But I'm looking for:


A way to listen for changes on the div tree (without making a request every 2 second)
Less code. 


Dependencies: 

Terminal-Notifier
BeautifulSoup
TimeLoop
Requests
Why would you use stackexchange.com and only take ones with the title of stackoverflow.com? That's a huge waste. 

You could change your url from https://stackexchange.com/questions?tab=realtime to https://stackoverflow.com/questions?tab=realtime.

However, actually visiting the website the way you are is hacky and overkill. Instead of web scraping, use the Stackexchange API.

There is no way to get a notification when a new question is posted. You will have to make a call every time you want the latest results.

Edit: there may be a way to get notifications, see vogel612's comment:


  there is websockets to receive notifications when a new question is
  posted. To my knowledge these are very much undocumented and easier to
  get access to with a userscript (which would imply changing to
  javascript), but it's not like there's no way to receive notifications
  from SE directly.