dimanche 29 décembre 2019

Not sure why this if statement's "else" fails to work

I am using BeautifulSoup/requests and Tkinter to web scrape stock values off of Yahoo Finances. I set up my program to scrape values if the user types words in the GUI entry box, and everything else should return a "Not Found".

However, there is a problem. My program follows the if statements properly, but with the "else" it fails to update. After spending a while looking through my code, I am not sure why this occurs.

If someone could help me, I would much appreciate it!

from bs4 import BeautifulSoup
import requests
import tkinter

headers = {"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) Apple Webkit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15"}


# *** Functions pertaining to the GUI application ***

# Resets the data fields after submission
def ResetData():
    user_company.delete('0', 'end')

#  *** Functions pertaining to the web scraping of Yahoo Finances ***

def ChooseValue():
    global output_value

    try:
        if user_company.get().upper() == "DOW JONES":
            getDOW()
        elif user_company.get().upper() == "APPLE":
            getApple()
        elif user_company.get().upper() == "NASDAQ":
            getNASDAQ()
        elif user_company.get().upper() == "S&P 500":
            getSP()
        else:
            output_value.config(text = "Not Found!", fg = "green")

    except Exception:
        output_value.config(text = "ERROR!", fg = "red")

    output_value.config(text = "$" + str(value), fg = "blue")
    ResetData()

# Extracts the DOW Jones Industrial Average
def getDOW():
    global value
    URL = "https://finance.yahoo.com/quote/^DJI?p=^DJI"
    r = requests.get(URL, headers = headers)

    soup = BeautifulSoup(r.content, "html.parser")

    value = soup.find("span", class_= "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").get_text()

# Exracts NASDAQ index 
def getNASDAQ():
    global value
    URL = "https://finance.yahoo.com/quote/^IXIC?p=^IXIC"
    r = requests.get(URL, headers = headers)

    soup = BeautifulSoup(r.content, "html.parser")

    value = soup.find("span", class_= "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").get_text()

# Extracts S&P 500 index
def getSP():
    global value
    URL = "https://finance.yahoo.com/quote/^GSPC?p=^GSPC"
    r = requests.get(URL, headers = headers)

    soup = BeautifulSoup(r.content, "html.parser")

    value = soup.find("span", class_= "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").get_text()

# Extracts Apple Inc. (AAPL)
def getApple():
    global value
    URL = "https://finance.yahoo.com/quote/AAPL?p=AAPL"
    r = requests.get(URL, headers = headers)

    soup = BeautifulSoup(r.content, "html.parser")

    value = soup.find("span", class_= "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").get_text()

def main():
    global user_company, output_value

    window = tkinter.Tk()
    window.title("Stock Value Search")

    output_value = tkinter.Label(window, text = "             ", fg = "blue")
    output_value.pack()

    user_company_label = tkinter.Label(window, text = "Enter company/index: ")
    user_company_label.pack()

    user_company = tkinter.Entry(window)
    user_company.pack()

    processButton = tkinter.Button(window, text = "Search", command = ChooseValue)
    processButton.pack()

    window.mainloop()

main()

Aucun commentaire:

Enregistrer un commentaire