Skip to main content
ALPHA    This is new software undergoing tests! Thank you for your patience.

Candidate: jeffreychan jeffreychan Presented by: Conrad Ho conrad Assessed by: Naomi Ceder nceder

Python (2023) ~ Grade 2 (Elementary)

Checking what clothes I should wear for the day

This project is my first project I can call my own. I hope I'm able to overcome the adversities I encounter along the way. I want this program to achieve to things: 1. to be able to scrape the current weather from an online website 2. to tell me what clothes I wear

Attached files
Filename (click to download) Size Uploaded by
weather_report.py 345 bytes jeffreychan
Markdown code
```Python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("https://www.hko.gov.hk/en/index.html")

current_temperature = driver.find_element(By.CLASS_NAME, 'hkoTemp').text

print('The current temperature is' ,current_temperature)
```
weather_report.py 1.3 KB jeffreychan
Markdown code
```Python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

clothes_dict = {
    '25 degrees or above' : {
    'bodywear' :"short seelved shirt" , 
    'legwear' :"shorts"},
    
    '24 degrees or lower' : {
    'bodywear' : "long seelved shirt",
    'legwear' : "long legwear"},
    
    '15 degrees or under' : "coat"
}

def find_current_temperature():
    driver = webdriver.Chrome()

    driver.get("https://www.hko.gov.hk/en/index.html")

    current_temperature = driver.find_element(By.CLASS_NAME, 'hkoTemp').text

    print('The current temperature is' ,current_temperature)

    if float(current_temperature) >= 25:
        print('you should wear {} and {}'.format(clothes_dict["25 degrees or above"]['bodywear'],clothes_dict['25 degrees or above']['legwear']))
    elif float(current_temperature) < 15:
        print('you should wear {}, {} and also a {}'.format(clothes_dict["24 degrees or lower"]['bodywear'], clothes_dict["24 degrees or lower"]['legwear'],clothes_dict['15 degrees or under']))
    else:
        print('you should wear {} and {}'.format(clothes_dict["24 degrees or lower"]['bodywear'], clothes_dict["24 degrees or lower"]['legwear']))


while True:
    find_current_temperature()
    time_minutes = 10
    time.sleep(time_minutes * 60)
```
weather_report.py 1.7 KB jeffreychan
Markdown code
```Python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

# This dictionary consists of the categories of clothings with the respective weather conditions
clothes_dict = {
    '25 degrees or above' : {
    'bodywear' :"short seelved shirt" , 
    'legwear' :"shorts"},
    
    '24 degrees or lower' : {
    'bodywear' : "long seelved shirt",
    'legwear' : "long legwear"},
    
    '15 degrees or under' : "coat"
}

# this function goes to the Hong Kong observitory website to scarpe the degree of the weather
# It also outputs the clothing recommendations

def find_current_temperature():
    driver = webdriver.Chrome()

    driver.get("https://www.hko.gov.hk/en/index.html")

    current_temperature = driver.find_element(By.CLASS_NAME, 'hkoTemp').text

    print('The current temperature is' ,current_temperature)

    if float(current_temperature) >= 25:
        print('you should wear {} and {}'.format(clothes_dict["25 degrees or above"]['bodywear'],clothes_dict['25 degrees or above']['legwear']))
    elif float(current_temperature) < 15:
        print('you should wear {}, {} and also a {}'.format(clothes_dict["24 degrees or lower"]['bodywear'], clothes_dict["24 degrees or lower"]['legwear'],clothes_dict['15 degrees or under']))
    else:
        print('you should wear {} and {}'.format(clothes_dict["24 degrees or lower"]['bodywear'], clothes_dict["24 degrees or lower"]['legwear']))

#the following while loop reruns the function + output every ten minutes

while True:
    find_current_temperature()
    time_minutes = 10
    time.sleep(time_minutes * 60)
```

Status: Submitted for assessment.


jeffreychan jeffreychan ~ 12 Mar 2023 12:37 a.m. (updated: 26 May 2023 7:46 a.m.)

After struggling for a while I've figured out how to scrape the information I want from the web I used Hong Kong observatory url:https://www.hko.gov.hk/en/index.html

The problem was mainly because I used BeautifulSoup to directly scrape the html code but the information I wanted was dynamic so I used selenium instead

Here's the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("https://www.hko.gov.hk/en/index.html")

current_temperature = driver.find_element(By.CLASS_NAME, 'hkoTemp').text

print('The current temperature is' ,current_temperature)

jeffreychan jeffreychan ~ 12 Mar 2023 10:08 a.m. (updated: 26 May 2023 8:18 a.m.)

I've added to things to the program:

  1. The suggested clothes to wear

The weather in Hong Kong mainly consists on really hot(Summer), hot but with wind (autumn, spring)and very very cold. (winter)

So from these three different weather conditions I decided to choose kinds of clothing for the body wear and leg wear.

Short sleeved shirts and shorts for summer.

Long sleeved shirts and any leg wear long enough to touch you're ankles (shortened to long legwear) for Autumn and Spring.

For winter, it'll be the same as Spring and Autumn but just with a coat on top of that.

  1. A loop to check the weather and renew the suggestions

Here's the code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

# This dictionary consists of the categories of clothings with the respective weather conditions
clothes_dict = {
    '25 degrees or above' : {
    'bodywear' :"short seelved shirt" , 
    'legwear' :"shorts"},

    '24 degrees or lower' : {
    'bodywear' : "long seelved shirt",
    'legwear' : "long legwear"},

    '15 degrees or under' : "coat"
}

# this function goes to the Hong Kong observitory website to scarpe the degree of the weather
# It also outputs the clothing recommendations

def find_current_temperature():
    driver = webdriver.Chrome()

    driver.get("https://www.hko.gov.hk/en/index.html")

    current_temperature = driver.find_element(By.CLASS_NAME, 'hkoTemp').text

    print('The current temperature is' ,current_temperature)

    if float(current_temperature) >= 25:
        print('you should wear {} and {}'.format(clothes_dict["25 degrees or above"]['bodywear'],clothes_dict['25 degrees or above']['legwear']))
    elif float(current_temperature) < 15:
        print('you should wear {}, {} and also a {}'.format(clothes_dict["24 degrees or lower"]['bodywear'], clothes_dict["24 degrees or lower"]['legwear'],clothes_dict['15 degrees or under']))
    else:
        print('you should wear {} and {}'.format(clothes_dict["24 degrees or lower"]['bodywear'], clothes_dict["24 degrees or lower"]['legwear']))

#the following while loop reruns the function + output every ten minutes

while True:
    find_current_temperature()
    time_minutes = 10
    time.sleep(time_minutes * 60)

nceder Naomi Ceder ~ 28 Jul 2023 1:29 a.m.

Hi,

I'll be assessing your project.

First of all, I like the idea - years ago I did something similar, but to know what the weather was going to be like when I went for a run. It's always cool to get the info direct from the source via your own code.

Congratulations on figuring out how to use selenium to get the contents of a dynamically generated page. This is a good technique to have in your toolbox, but probably one that should be used only if absolutely necessary.

I'd be interested if there are aspects of using selenium that you see as potential disadvantages?

In fact, I wouldn't use selenium myself. For something like weather, most sites have other options besides just the dynamic web page that most people use. In this case, I found a bunch of other options. For your level of experience and for your application, probably the easiest would be the text based version of the page.

If you go to this link: https://www.hko.gov.hk/textonly/v2/index.htm you can see the list of text based pages available. The one that has info similar to the page you get is at https://www.hko.gov.hk/textonly/v2/forecast/englishwx2.htm.

If you wanted to use that text based page, what changes would you make? And what would you see as the benefits of using that page, if any?

Cheers, Naomi


Back to top