NameError:全局名称"NAME"未定义 [英] NameError: global name 'NAME' is not defined

查看:67
本文介绍了NameError:全局名称"NAME"未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构建一个小型Web抓取工具上度过了一段有趣的时光,我认为我在变量或函数范围方面做错了什么.每当我尝试将某些功能提取到单独的功能中时,都会给我NameError:全局名称"NAME"未定义.我看到很多人都遇到了类似的问题,但是似乎有很多相同的错误出现,我无法弄清楚.

I have been having an interesting time building a little web scraper and I think I am doing something wrong with my variable or function scope. Whenever I try to pull out some of the functionality into separate functions it gives me the NameError: global name 'NAME' is not defined. I see that a lot of people are having a similar problem but there seems to be a lot of variation with the same error and I can't figure it out.

import urllib2, sys, urlparse, httplib, imageInfo
from BeautifulSoup import BeautifulSoup
from collections import deque

global visited_pages
visited_pages = []
global visit_queue
visit_queue = deque([])
global motorcycle_pages
motorcycle_pages = []
global motorcycle_pics
motorcycle_pics = []

global count 
count = 0

def scrapePages(url):
    #variables
    max_count = 20
    pic_num = 20

    #decide how long it should go on...
    global count
    if count >= max_count:
        return

    #this is all of the links that have been scraped
    the_links = []

    soup = soupify_url(url)

    #find all the links on the page
    for tag in soup.findAll('a'):
        the_links.append(tag.get('href'))


    visited_pages.append(url)
    count = count + 1
    print 'number of pages visited'
    print count

    links_to_visit = the_links
#    print 'links to visit'
#    print links_to_visit

    for link in links_to_visit:
        if link not in visited_pages:
            visit_queue.append(link)
    print 'visit queue'
    print visit_queue

    while visit_queue:
        link = visit_queue.pop()
        print link
        scrapePages(link)

    print '***done***'


the_url = 'http://www.reddit.com/r/motorcycles'
#call the function
scrapePages(the_url)


def soupify_url(url):
    try:
        html = urllib2.urlopen(url).read()
    except urllib2.URLError:
        return 
    except ValueError:
        return
    except httplib.InvalidURL:
        return
    except httplib.BadStatusLine:
        return

    return BeautifulSoup.BeautifulSoup(html) 

这是我的引用:

Traceback (most recent call last):
  File "C:\Users\clifgray\Desktop\Mis Cosas\Programming\appengine\web_scraping\src\test.py", line 68, in <module>
    scrapePages(the_url)
  File "C:\Users\clifgray\Desktop\Mis Cosas\Programming\appengine\web_scraping\src\test.py", line 36, in scrapePages
    soup = soupify_url(url)
NameError: global name 'soupify_url' is not defined

推荐答案

移动您的主要代码:

the_url = 'http://www.reddit.com/r/motorcycles'
#call the function
scrapePages(the_url)

在定义 soupify_url 的点之后,即文件的底部.

After the point where you define soupify_url, ie. the bottom of your file.

Python正在读取def scrapePages()的定义,然后尝试调用它; scrapePages()想要调用一个名为 soupify_url()的函数,该函数尚未定义,因此您将获得:

Python is reading that def scrapePages() is defined, then it tries to call it; scrapePages() wants to call a function called soupify_url() which has not yet been defined thus you're getting a:

NameError: global name 'soupify_url' is not defined

请牢记以下规则:所有功能必须在任何实际起作用的代码之前定义

如果在 soupify_url()的定义之后将调用 scrapePages()的主要代码移到,则所有内容都将被定义并在范围内,应该可以解决您的错误.

If you move your main code calling scrapePages() to after the definition of soupify_url() everything will be defined and in scope, should resolve your error.

这篇关于NameError:全局名称"NAME"未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆