Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatBot/alternativeWeather.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
26 lines (23 sloc)
1.15 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Programmed by Premo | |
#Simple weather program gets the current temperature in a given city directly | |
#from a given website and returns the information it found from a given part | |
#of the same website | |
from selenium import webdriver | |
#Selenium web driver imported | |
def weather(city): | |
#The instance of chrome Web driver is created | |
driver = webdriver.Chrome() | |
day = str(input("What day would you like to know the forecast for?")).lower() | |
#driver will get | |
driver.get("https://www.timeanddate.com/weather/uk/"+city+"") | |
#Program can only get weather forecast for the day if the day is not today it will return nothing | |
if "today" in day: | |
#the element we require will be found by its class name which we have defined | |
#the class name can be located by inspecting html source of the webpage | |
#and finding the relevant region | |
return("It is " + driver.find_elements_by_class_name("h2")[0].text + " now.") | |
else: | |
return None | |
#Prompts the user to put in a given city which will be used in the function defined above | |
city = str(input("Which city would you like me to check?")).lower() | |
weather(city) |