D
D
Dizzy Qucile2020-04-27 07:12:33
Python
Dizzy Qucile, 2020-04-27 07:12:33

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 5ea658b000f27870642140.png

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] + "%")


If the span contains "Probability of precipitation 70%", then it is successfully parsed, and if it is just the words "No precipitation", then it gives an error:
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


The entire code of the parser itself:
spoiler
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()


How can I fix the error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-04-27
@DiZiNnEs

Read the tutorial, understand about variable scopes and declare the output variable in the same block where it is used.

G
galaxy, 2020-04-27
@galaxy

In the second case, there are no two nested spans, and the line output = [e.strip() for e in first if not e.name and e.strip()] is missing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question