문제

website = raw_input('website: ')
with open('words.txt', 'r+') as arquivo:
    for lendo in arquivo.readlines():
        msmwebsite = website + lendo
        try:
            abrindo = urllib2.urlopen(msmwebsite)
            abrindo2 = abrindo.read()           

        except URLError as e:
            pass

        if abrindo.code == 200:
            palavras = ['registration', 'there is no form']
            for palavras2 in palavras:
                if palavras2 in abrindo2:
                    print msmwebsite, 'up'

                else:
                    pass

        else:
            pass

It's working but for some reason, some websites I got this error:

if abrindo.code == 200:
NameError: name 'abrindo' is not defined

How to fix it? .......................................................................................................................................................................................

도움이 되었습니까?

해결책

Replace pass with continue. And at least do some error logging, as you silently skip erroneous links.

In case your request resulted in an URLError, no variable abrindo is defined, hence your error.

다른 팁

abrindo is created only in the try block. It will not be available if the catch block is executed. To fix this, move the block of code starting with

if abrindo.code == 200:

inside the try block. One more suggestion, if you are not doing anything in the else part, instead of explicitly writing that with pass, simply remove them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top