From 604a90065891050df4b9bfdebd882ca222a6b205 Mon Sep 17 00:00:00 2001 From: MizterB <5458030+MizterB@users.noreply.github.com> Date: Fri, 21 Jan 2022 05:17:48 -0500 Subject: [PATCH] Scrape HTML attributes that are not key/val pairs (#58247) --- homeassistant/components/scrape/sensor.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/scrape/sensor.py b/homeassistant/components/scrape/sensor.py index 4cb6975f0f0..c0523e4cbe2 100644 --- a/homeassistant/components/scrape/sensor.py +++ b/homeassistant/components/scrape/sensor.py @@ -160,14 +160,23 @@ class ScrapeSensor(SensorEntity): raw_data = BeautifulSoup(self.rest.data, "html.parser") _LOGGER.debug(raw_data) - if self._attr is not None: - value = raw_data.select(self._select)[self._index][self._attr] - else: - tag = raw_data.select(self._select)[self._index] - if tag.name in ("style", "script", "template"): - value = tag.string + try: + if self._attr is not None: + value = raw_data.select(self._select)[self._index][self._attr] else: - value = tag.text + tag = raw_data.select(self._select)[self._index] + if tag.name in ("style", "script", "template"): + value = tag.string + else: + value = tag.text + except IndexError: + _LOGGER.warning("Index '%s' not found in %s", self._attr, self.entity_id) + value = None + except KeyError: + _LOGGER.warning( + "Attribute '%s' not found in %s", self._attr, self.entity_id + ) + value = None _LOGGER.debug(value) return value