NameError: global name 'NAME' is not defined

好久不见. 提交于 2021-02-08 07:04:14

问题


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) 

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

回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/14751688/nameerror-global-name-name-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!