def _today_forecast(self, args): criteria = { 'today_nowcard-temp': 'div', 'today_nowcard-phrase': 'div', 'today_nowcard-hilo': 'div', } content = self._request.fetch_data(args.forecast_option.value, args.area_code) bs = BeautifulSoup(content, 'html.parser') container = bs.find('section', class_='today_nowcard-container') weather_conditions = self._parse(container, criteria) if len(weather_conditions) < 1: raise Exception('Could not parse weather forecast for today.') weatherinfo = weather_conditions[0] temp_regex = re.compile(('H\s+([0-9]+|\-{,2}).+' 'L\s+([0-9]+|\-{,2})')) temp_info = temp_regex.search(weatherinfo['today_nowcard-hilo']) high_temp, low_temp = temp_info.groups() side = container.find('div', class_='today_nowcard-sidecar') humidity, wind = self._get_additional_info(side) curr_temp = self._clear_str_number(weatherinfo['today_nowcard-temp']) self._unit_converter.dest_unit = args.unit td_forecast = Forecast(self._unit_converter.convert(curr_temp), humidity, wind, high_temp=self._unit_converter.convert(high_temp), low_temp=self._unit_converter.convert(low_temp), description=weatherinfo['today_nowcard-phrase']) return [td_forecast]
def _prepare_data(self, results, args): forecast_result = [] self._unit_converter.dest_unit = args.unit for item in results: match = self._temp_regex.search(item['temp']) if match is not None: high_temp, low_temp = match.groups() try: dateinfo = item['weather-cell'] date_time, day_detail = dateinfo[:3], dateinfo[3:] item['date-time'] = date_time item['day-detail'] = day_detail except KeyError: pass day_forecast = Forecast( self._unit_converter.convert(item['temp']), item['humidity'], item['wind'], high_temp=self._unit_converter.convert(high_temp), low_temp=self._unit_converter.convert(low_temp), description=item['description'].strip(), forecast_date=f'{item["date-time"]} {item["day-detail"]}', forecast_type=self._forecast_type) forecast_result.append(day_forecast) return forecast_result
def _prepare_data(self, results, args, forecast_size): forecast_results = [] self._unit_converter.dest_unit = args.unit length = 0 for item in results: if (length >= forecast_size): break length += 1 match = self._temp_regex.search(item['temp']) if (match is not None): high_temp, low_temp = match.groups() day_forecast = Forecast( self._unit_converter.convert(item['temp']), item['humidity'], item['wind'], high_temp=self._unit_converter.convert(high_temp), low_temp=self._unit_converter.convert(low_temp), description=item['description'].strip(), forecast_date=f'{item["day-detail"]}', forecast_type=self._forecast_type) forecast_results.append(day_forecast) return forecast_results
def _today_forecast(self, args): """returns weather parses for the day""" # contains the DOM elements that want to find in the HTML of the weather website for today's scraping # key is the name of the CSS class and value is the type of HTML criteria = { 'today_nowcard-temp': 'div', # CSS class containing current temperature 'today_nowcard-phrase': 'div', # CSS class containing weather conditions text for description 'today_nowcard-hilo': 'div', # CSS class containing highest and lowest temperature } content = self._request.fetch_data(args.area_code, args.forecast_option.value) bs = BeautifulSoup( content, 'html.parser') # as bs object of the page is returned # container is the section tag on weathercom website that holds most of the info on weather container = bs.find('section', class_='today_nowcard-container') # to find elements in children or subtags of container that are in criteria to retrieve/scrape them weather_conditions = self._parse(container, criteria) # if len of weather_conditions is less than 1, no info was obtained or scraped if len(weather_conditions) < 1: raise Exception('Could not parse weather for today') weather_info = weather_conditions[0] temp_regex = re.compile(('H\s+(\d+|\-{,2}).+' 'L\s+(\d+|\-{,2})')) temp_info = temp_regex.search(weather_info['today_nowcard-hilo']) high_temp, low_temp = temp_info.groups() # getting wind and humidity info side = container.find('div', class_='today_nowcard-sidecar') humidity, wind = self._get_humidity_and_weather(side) # getting current temp current_temperature = self._clear_str_number( weather_info['today_nowcard-temp']) # set default unit to the value of args.unit attribute self._unit_converter.dest_unit = args.unit today_forecast = Forecast( self._unit_converter.convert(current_temperature), humidity, wind, self._unit_converter.convert(high_temp), self._unit_converter.convert(low_temp), description=weather_info['today_nowcard-phrase']) # return today_forecast object as a list return [today_forecast]
def _today_forecast(self, args): criteria = { 'CurrentConditions--tempValue--3KcTQ': 'span', 'CurrentConditions--phraseValue--2xXSr': 'div', 'CurrentConditions--tempHiLoValue--A4RQE': 'div', } content = self._request.fetch_data(args.forecast_option.value, args.area_code) bs = BeautifulSoup(content, 'html.parser') container = bs.find('section', class_='card') # print(container) weather_conditions = self._parse(container, criteria) if len(weather_conditions) < 1: raise Exception('Could not parse weather forecast for today.') weatherinfo = weather_conditions[0] # for weather in weather_conditions: # print(weather) temp_regex = re.compile(('([0-9]+|\-{,2}).+' '/([0-9]+|\-{,2})')) temp_info = temp_regex.search( weatherinfo['CurrentConditions--tempHiLoValue--A4RQE']) high_temp, low_temp = temp_info.groups() # side = container.find('div', class_='today_nowcard-sidecar') # wind, humidity = self._get_additional_info(side) curr_temp = self._clear_str_number( weatherinfo['CurrentConditions--tempValue--3KcTQ']) self._unit_converter.dest_unit = args.unit td_forecast = Forecast( self._unit_converter.convert(curr_temp), humidity=0, wind=0, high_temp=self._unit_converter.convert(high_temp), low_temp=self._unit_converter.convert(low_temp), description=weatherinfo['CurrentConditions--phraseValue--2xXSr']) return [td_forecast]
def _today_forecast(self, args): bs = self._make_http_request(args) container = bs.find("div", {"data-testid": "CurrentConditionsContainer"}) criteria = [ ('span', 'data-testid', 'TemperatureValue'), ('div', 'data-testid', 'wxPhrase'), ('div', 'class', 'tempHiLoValue') ] weather_conditions = self._parse(container, criteria) if len(weather_conditions) < 1: raise Exception('Could not parse weather forecast for today.') weatherinfo = weather_conditions[0] curr_temp = self._clear_str_number(weatherinfo['TemperatureValue']) temp_info = weatherinfo['tempHiLoValue'].split('/') high_temp = self._clear_str_number(temp_info[0]) low_temp = self._clear_str_number(temp_info[1]) # Parses the "Weather Today in {location}" card for wind & humidity details_container = bs.find("section", { "data-testid" : "TodaysDetailsModule"}) wind = details_container.find("span", {"data-testid":"Wind"}).get_text() humidity = details_container.find("span", {"data-testid": "PercentageValue"}).get_text() # Determine wind direction by degree angle # wind_direction_style = details_container.find("svg", {"name": "wind-direction"})["style"] # degrees = re.findall(r'\d+', wind_direction_style) # TODO: Determine cardinality by angle: 0 - S, 90 - W, 180 - N, 270 - E # wind = cardinality + " " + wind self._unit_converter.dest_unit = args.unit td_forecast = Forecast( self._unit_converter.convert(curr_temp), humidity, wind, high_temp = self._unit_converter.convert(high_temp), low_temp = self._unit_converter.convert(low_temp), description = weatherinfo['wxPhrase'] ) return [td_forecast]
def _prepare_data(self, results, args): # Used by 5day/10day/weekend forecasts to further parse data, then return Forecast objects forecast_result = [] self._unit_converter.dest_unit = args.unit for item in results: try: high_temp, low_temp = re.findall(r'\d+', item['temp']) except: high_temp = 0 low_temp = re.findall(r'\d+', item['temp'])[0] # specific to weekend forecast markup try: dateinfo = item['weather-cell'] date_time, day_detail = dateinfo[:3], dateinfo[3:] item['date-time'] = date_time item['day-detail'] = day_detail except KeyError: pass if 'day-detail' not in item: item['day-detail'] = '' day_forecast = Forecast( self._unit_converter.convert(item['temp']), item['humidity'], item['wind'], high_temp = self._unit_converter.convert(high_temp), low_temp = self._unit_converter.convert(low_temp), description = item['description'].strip(), forecast_date = f'{item["date-time"]} {item["day-detail"]}', forecast_type = self._forecast_type ) forecast_result.append(day_forecast) return forecast_result