Coroutines in Python

Note: Get your free API key at https://newsapi.org/ by first making your account.
    

   from urllib.request import urlopen
from bs4 import BeautifulSoup

def searcher():
    url = "https://en.wikipedia.org/wiki/Kabul#Toponymy_and_etymology"
    page = urlopen(url)
    html = page.read().decode("utf-8")
    soup = BeautifulSoup(html, "html.parser")
    article1 = soup.get_text()
    
    url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
    page = urlopen(url)
    html = page.read().decode("utf-8")
    soup = BeautifulSoup(html, "html.parser")
    article2 = soup.get_text()
    
    url = "https://en.wikipedia.org/wiki/Amoeba_(operating_system)"
    page = urlopen(url)
    html = page.read().decode("utf-8")
    soup = BeautifulSoup(html, "html.parser")
    article3 = soup.get_text()
    
    article = article1 + article2 + article3
    
    while True:
        word = (yield)
        if word in article:
            print("The word is in the article.")
        else:
            print("The word is NOT in the article.")

            
search = searcher() # generator instance
print("Search initiated.")
next(search) # starting the generator
print("Search finito.")
input("Press any key then enter to get results: ")
search.send("Maiwand")
search.send("Salim Durani")
search.send("Babrak Karmal")
search.send("Babrak Karmal2")
search.send("Salim Durani2")
search.send("Abdur Rehman")
search.send("Python was conceived in the late 1980s")
search.send("""Early versions used a "homebrew" window system""")
search.send("in")
search.close()

    

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes