Парсер на Python (выбор по тегам)

День добрый. Пытаюсь (учусь) сделать парсинг по сайту. Есть данные (таких строчек с данными много, примерно 28-30 ед.).

    <div class="maxt">
    <temperature-value value="-12" from-unit="c" reactive="">−12</temperature-value>
    </div>

Получаю строки через BeautifulSoup.

maxt = bs.find_all('div', class_= "maxt")

Сами строки корректные.

Не получается из это строки получить значение: -12 Для всего.

Подскажите, что не так.

Получилось сделать так, но может есть через "find...find_all"

temp_list = []
for t in temp:
    temperatyre = t['value']
    if temperatyre != '':
        print(t['value'])
        temp_list.append(temperatyre)

Дополнительно код рабочий, но выборка не устраивает (выбирает все температуры, а нужны например, только maxt и mint)

import requests
from bs4 import BeautifulSoup
 
url = 'https://www.gismeteo.ru/weather-omsk-4578/month/'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",}

response = requests.get(url, headers=headers)

bs = BeautifulSoup(response.text, 'lxml')
temp = bs.find_all('temperature-value')

print('всего строк = ' + str(len(temp)))

temp_list = []

for t in temp:
    temperatyre = t['value']
    if temperatyre != '':
        print(t['value'])
        temp_list.append(temperatyre)

Ответы (2 шт):

Автор решения: strawdog

Что-то вы намудрили немного. Хотите найти maxt/mint, а ищете temperature-value для всех значений. проще так (для maxt, например):

import requests
from bs4 import BeautifulSoup
 
url = 'https://www.gismeteo.ru/weather-omsk-4578/month/'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",}

response = requests.get(url, headers=headers)

bs = BeautifulSoup(response.text, 'lxml')
maxt = bs.find_all('div', class_= "maxt")

temp_list = []
for t in maxt:
    temps = t.find('temperature-value')
    temp_list.append(temps["value"])

print(temp_list)

['-15', '-12', '-9', '-6', '-6', '-5', '-4', '-11', '-7', '-6', '-8', '-10', '-12', '-13', '-13', '-13', '-14', '-12', '-13', '-14', '-15', '-13', '-14', '-13', '-14', '-12', '-13', '-12']

→ Ссылка
Автор решения: Pak Uula

Вам нужна выборка только по одному тегу - либо mint, либо maxt?

Нет ничего проще ))

from bs4 import BeautifulSoup
 
def get_temp_list(bs:BeautifulSoup, min_or_max: str) -> list[str]:
    if not min_or_max in ("mint" , "maxt"):
        raise ValueError(f"unsupported temperature class: {min_or_max}")
    t_list = bs.find_all('div', {'class': min_or_max})

    temp_list = []

    for mt in t_list:
        t = mt.find("temperature-value")
        if t is not None:
            temperatyre = t['value']
            if temperatyre != '':
                temp_list.append(temperatyre)
    return temp_list

Результат для ваших данных

url = 'https://www.gismeteo.ru/weather-omsk-4578/month/'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",}

response = requests.get(url, headers=headers)

parsed = BeautifulSoup(response.text, 'lxml')

mint = get_temp_list(parsed, "mint")
maxt = get_temp_list(parsed, "maxt")

print(",".join(mint))
print(",".join(maxt))
-22,-23,-10,-10,-9,-10,-9,-18,-14,-14,-12,-12,-12,-12,-13,-12,-14,-15,-16,-16,-18,-18,-18,-17,-17,-15,-15,-17,-17,-17,-15,-14,-14,-16,-15
-15,-12,-9,-6,-6,-4,-5,-12,-9,-9,-7,-9,-10,-11,-11,-11,-13,-14,-15,-15,-17,-16,-16,-16,-14,-13,-14,-15,-15,-14,-13,-12,-13,-14,-13
→ Ссылка