I
I
ivan97272022-02-16 15:24:38
Python
ivan9727, 2022-02-16 15:24:38

UnboundLocalError: local variable referenced before assignment what to do?

Good afternoon, there is a parser that comes in to log in, collects information, sorts out the keys, but when sending a message to tg, an error occurs if task_stack not in known_tasks and key in task_key:
UnboundLocalError: local variable 'task_stack' referenced before assignment
tried to set global
Please tell me which way to dig .

def send_telegram(message, task):
    bot.send_message(message.chat.id, task)
def func (message):
    if (message.text == 'Включить Парсер'):
        bot.send_message(message.chat.id, "Запускаю парсер")
        leads = "n.php"
        main_key = config.KEY_WORDS
        url = config.URL + leads
        driver = webdriver.Firefox()
        driver.get(url)
        login = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/label/span/input')
        login.send_keys(config.LOGIN)

        time.sleep(0.1)

        passw = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/div/div/div[2]/label/span/input')
        passw.send_keys(config.PASSWORD)

        click = driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div/div/form/a')
        click.click()
        
        time.sleep(4)
        page = driver.page_source

        soup = bs(page, 'html.parser')
        known_tasks = []
        while True:
            for block in soup.find_all(class_=re.compile("OrderSnippetContainerStyles")):
                task_key = block.find(class_=re.compile("SnippetBodyStyles__Content")).get_text().replace('\n', ' ')
                name = block.find(class_=re.compile("StatusAndClientInfoStyles__Name")).get_text().replace('\n', ' ')
                task_stack = (task_key, name)
            for key in main_key:
                if task_stack not in known_tasks and key in task_key:
                    known_tasks.append(task_key)
                    send_telegram(str(''.join(task_stack)))
                known_tasks.append(task_stack)
            time.sleep(60)
            driver.get(url)
            page = driver.page_source
            soup = bs(page, 'html.parser')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2022-02-16
@fox_12

In this line

for block in soup.find_all(class_=re.compile("OrderSnippetContainerStyles"))

if an empty set arrives in soup.find_all(class_=re.compile("OrderSnippetContainerStyles")) , then task_stack is not initialized, because the loop body will not be executed, and this error will be thrown.
Here in this direction and dig.

V
Vindicar, 2022-02-16
@Vindicar

If the loop for block in soup.find_allnever executes (for example, there are no matching elements), then task_stack will not be assigned a value. And you continue to read it.
Provide some default value for this variable, and check for such a case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question