Answer the question
In order to leave comments, you need to log in
How to correctly give a condition that there is no "precipitation"?
Hello, there is a site https://weather.rambler.ru/v-nur-sultane/3-days/ , you need to parse
Here is my code element that parses this very place:
for first in precipitation.select('span'):
for second in first.select('span'):
output = [e.strip() for e in first if not e.name and e.strip()]
print(output[0] + "%")
Traceback (most recent call last):
File "/home/dizinnes/PycharmProjects/weatherbot/test.py", line 38, in <module>
nursultan_tomorrow_parser()
File "/home/dizinnes/PycharmProjects/weatherbot/test.py", line 33, in nursultan_tomorrow_parser
Осадки: {output[0]}%
UnboundLocalError: local variable 'output' referenced before assignment
import requests
from bs4 import BeautifulSoup
def nursultan_tomorrow_parser():
r = requests.get('https://weather.rambler.ru/v-nur-sultane/3-days/')
html = BeautifulSoup(r.content, 'html.parser')
for tomorrow in html.select('div._18iJ:nth-child(3)'):
night_temperature = tomorrow.select_one('span._3k_D:nth-child(2)').text
for determine_temperature in tomorrow.select('div._3wdR:nth-child(2)'):
day_temperature = determine_temperature.select_one('span._3k_D').text
for wind in tomorrow.select('div._1Y0B.wind'):
wind_result = wind.select_one('span._1DZh').text.lower()
for precipitation in tomorrow.select('div._1Y0B.precipitationProbability'):
# preciptation_result = precipitation.select_one('span._1DZh').content
for first in precipitation.select('span'):
for second in first.select('span'):
output = [e.strip() for e in first if not e.name and e.strip()]
# e = []
# for e in test2:
# if not e.name and e.strip:
# pass
# else:
# e = ['без осадков']
result = f'''\
Прогноз погоды на завтра:
Температура ночью: {night_temperature}
Температура днём: {day_temperature}
Скорость ветра: {wind_result}
Осадки: {output[0]}%
'''
print(result)
nursultan_tomorrow_parser()
Answer the question
In order to leave comments, you need to log in
Read the tutorial, understand about variable scopes and declare the output variable in the same block where it is used.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question