Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Console weather display

menator01

Gold Coder
Although there are better ways to do this but, just playing around. 🙂

Python:
import asyncio
from bs4 import BeautifulSoup
import requests
from datetime import datetime, timedelta
import sys


USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
# US english
LANGUAGE = "en-US,en;q=0.5"

def get_weather():
    ''' function for calling google weather and getting the data '''
    session = requests.Session()
    session.headers['User-Agent'] = USER_AGENT
    session.headers['Accept-Language'] = LANGUAGE
    session.headers['Content-Language'] = LANGUAGE
    url = 'https://www.google.com/search?q=weather'

    html = session.get(url)

    soup = BeautifulSoup(html.text, 'html.parser')

    # Create a dict to hold the data and get wanted data
    result = {}
    result['Region'] = soup.find('span', attrs={'class': 'BBwThe'}).text
    result['Temperature'] = soup.find('span', attrs={'id': 'wob_tm'}).text
    result['Conditions'] = soup.find('span', attrs={'id': 'wob_dc'}).text
    result['Humidity'] = soup.find('span', attrs={'id': 'wob_hm'}).text
    result['Percipitation'] = soup.find('span', attrs={'id': 'wob_pp'}).text
    result['Wind'] = soup.find('span', attrs={'id': 'wob_ws'}).text
    

    return result


async def show():
    data = get_weather()
    count = len(data)+1

    # Call the
    minutes = 2

    now = datetime.now()
    later = now+timedelta(minutes=minutes)

    while True:
        now = datetime.now()
        await asyncio.sleep(1)
        print(f'''Current Time: {str(now.hour).zfill(2)}:{str(now.minute).zfill(2)}:{str(now.second).zfill(2)} {now.strftime("%p")}''')
    
        for key, value in data.items():
            if key == 'Temperature':
                value = f'{value}\u00b0 F'
            print(f'{key}: {value}')
        sys.stdout.write('\x1b[1A'*count)
        if now >= later:
            data = get_weather()
            now = datetime.now()
            later = now+timedelta(minutes=minutes)

def run():
    try:
        asyncio.run(show())
    except KeyboardInterrupt:
        print(f'Exiting program')


if __name__ == '__main__':
    run()

Output
Code:
Current Time: 10:54:48 AM
Region: Warrior, AL     
Temperature: 88° F       
Conditions: Mostly cloudy
Humidity: 74%
Percipitation: 15%       
Wind: 4 mph
 

Latest posts

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom