Skip to main content
 首页 » 编程设计

Python,urllib2 模块中的 NameError,但仅在少数网站中

2025年05月04日14mfryf

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 

它正在工作,但出于某种原因,我在某些网站上遇到了这个错误:

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

如何解决? ..................................................... ..................................................... ..................................................... .....................................

请您参考如下方法:

将 pass 替换为 continue。至少做一些错误记录,因为你默默地跳过错误的链接。

如果您的请求导致 URLError,则没有定义变量 abrindo,因此您的错误。