Python NameError:未定义全局名称“NAME”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14751688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:19:40  来源:igfitidea点击:

NameError: global name 'NAME' is not defined

pythonfunctionweb-scrapingnameerror

提问by clifgray

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.

我在构建一个小网络抓取工具的过程中度过了一段有趣的时光,我认为我的变量或函数范围有问题。每当我尝试将某些功能提取到单独的函数中时,它都会给我 NameError: global name 'NAME' is not defined。我看到很多人都遇到了类似的问题,但似乎有很多变化与相同的错误,我无法弄清楚。

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) 

Here is my trackback:

这是我的引用:

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

采纳答案by Mike

Move your main code:

移动您的主要代码:

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

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

在您定义的点之后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:

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

NameError: global name 'soupify_url' is not defined

Keep in mind the rule: All functions must be defined before any code that does real work

牢记规则:所有函数必须在执行实际工作的代码之前定义

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.

如果您将主代码调用移动scrapePages()soupify_url()所有内容的定义都将被定义并在范围内,则应解决您的错误。