0

Hey so I need to web scrape this website (don't use beautiful soup) to get the current temperature and I am having trouble. This is what I have so far but I keep getting either a number that isn't the temperature or -1. So any help is greatly appreciated.

def assign4(city_name):
import urllib.request

if city_name == "St. Catharines":
    connection = urllib.request.urlopen("https://weather.gc.ca/city/pages/on-107_metric_e.html")
    condition = str(connection.read(), "utf-8")
    connection.close()

    weather_condition = condition.find("Temperature:</dt>")
    if weather_condition != -1:
        weather_condition_end = condition.find("</dd>",weather_condition)
        if weather_condition_end != -1:
            weather_start = condition.find("metric-hide",0,weather_condition_end)
            if weather_start != -1:
                print(f"Weather Conditions in St. Catharines is {weather_start}")
            else:
                print("'weather_start' not working")
        else:
            print("'weather_condition_end' not working")
    else:
        print("'weather_condition' not working")
assign4("St. Catharines")
3
  • Could you provide something that we can test? Commented Nov 16, 2020 at 15:48
  • Because weather_start is a POSITION in the string Commented Nov 16, 2020 at 15:49
  • Oh okay I see what you mean alex.... I will try different things. Commented Nov 16, 2020 at 16:13

2 Answers 2

1

There should be a space in between St. and Catherines in the last line. That is where it's wrong.

if city_name == "St. Catharines": assign4("St.Catharines")

When you are calling the function your are not adding the space.

Sign up to request clarification or add additional context in comments.

2 Comments

No the space is just that I forgot to put it in when I type that out.
@madisongrant Try and add "expected output" in the question ..so people can know what exactly you want the code to do. As you advance, this would help in getting the right answer sooner. Happy coding.
0

You can simplify your code with lxml and requests

import requests
from lxml import html
def assign4(city_name):
  if city_name == "St.Catharines":
      # Get the html page
      resp=requests.get("https://weather.gc.ca/city/pages/on-107_metric_e.html")
      # Build html tree
      html_tree=html.fromstring(resp.text)
      # Get temperature
      temperature=html_tree.xpath("//dd[@class='mrgn-bttm-0 wxo-metric-hide'][(parent::dl[@class='dl-horizontal wxo-conds-col2'])]//text()")[0].replace("Â", "")
      # Print temperature
      print(f"Temperature in {city_name} is {temperature}C")
  

assign4("St.Catharines")

Outputs:

>>> Temperature in St.Catharines is 4.8°C

7 Comments

I don't think rewriting the whole program differently is what SO is for. :) ..Kind of skips the learning curve for the OP. We stick to answering the specific issue OP raises.
@AbrarAhmed he didn't ask for a specific solution. "Any help" to get the temperature. The only restriction was not using beautifulsoup, which I didn't. But I get your point.
@madisongrant If you find it suitable for your problem, please mark it as the solution :)
Done so already I just have to make sure it is okay to use but I understand what was done basically done a path where the temperature is between the class and the parent and I greatly appreciated
@madisongrant Yes. If you need a deeper understanding of how it works, search about xpath.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.