Catch exception throws UnboundLocalError

I wrote a crawler to extract information from the Q & A website. Since not all fields are presented on the page all the time, I used several trial exceptions to handle the situation.

def answerContentExtractor( loginSession, questionLinkQueue , answerContentList) :
    while True:
        URL = questionLinkQueue.get()
        try:
            response   = loginSession.get(URL,timeout = MAX_WAIT_TIME)
            raw_data   = response.text

            #These fields must exist, or something went wrong...
            questionId = re.findall(REGEX,raw_data)[0]
            answerId   = re.findall(REGEX,raw_data)[0]
            title      = re.findall(REGEX,raw_data)[0]

        except requests.exceptions.Timeout ,IndexError:
            print >> sys.stderr, URL + " extraction error..."
            questionLinkQueue.task_done()
            continue

        try:
            questionInfo = re.findall(REGEX,raw_data)[0]
        except IndexError:
            questionInfo = ""

        try:
            answerContent = re.findall(REGEX,raw_data)[0]
        except IndexError:
            answerContent = ""

        result = {
                  'questionId'   : questionId,
                  'answerId'     : answerId,
                  'title'        : title,
                  'questionInfo' : questionInfo,
                  'answerContent': answerContent
                  }

        answerContentList.append(result)
        questionLinkQueue.task_done()

And this code, sometimes, may or may not, gives the following exception at runtime:

UnboundLocalError: local variable 'IndexError' referenced before assignment

The line number indicates that an error occurs in the second except IndexError:

Thank you all for your suggestions, I would like to give the brands you deserve, too bad, I can only point out one correct answer ...

+4
source share
3 answers

I think the problem is in this line:

except requests.exceptions.Timeout ,IndexError

This is equivalent to:

except requests.exceptions.Timeout  as IndexError:

, IndexError , requests.exceptions.Timeout. :

try:
    true
except NameError, IndexError:
    print IndexError
    #name 'true' is not defined

, :

except (requests.exceptions.Timeout, IndexError):

UnboundLocalError, IndexError , , UnboundLocalError.

>>> 'IndexError' in answerContentExtractor.func_code.co_varnames
True

, (requests.exceptions.Timeout ,IndexError), IndexError, , UnboundLocalError. :

def func():
    try:
        print
    except NameError, IndexError:
        pass
    try:
        [][1]
    except IndexError:
        pass
func()
#UnboundLocalError: local variable 'IndexError' referenced before assignment
+6

except requests.exceptions.Timeout ,IndexError:

Python requests.exceptions.Timeout, IndexError. -

except (requests.exceptions.Timeout ,IndexError) as e:
+2
except requests.exceptions.Timeout ,IndexError:

, except requests.exceptions.Timeout as IndexError

except (requests.exceptions.Timeout, IndexError):

+1

Source: https://habr.com/ru/post/1528040/


All Articles