Question

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? .......................................................................................................................................................................................

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top