menator01
Gold Coder
Although there are better ways to do this but, just playing around. 🙂
Output
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