2013-02-07 125 views
1

我一直有一個有趣的時間建立一個小網站刮板,我覺得我做錯了什麼與我的變量或函數範圍。每當我嘗試將一些功能提取到單獨的函數中時,它會給我NameError:全局名稱'NAME'未定義。我發現很多人都遇到類似的問題,但是出現同樣的錯誤似乎有很多變化,我無法弄清楚。NameError:全局名稱「NAME」沒有定義

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 
+0

_Where_你得到的錯誤?在哪條線上? –

+0

剛添加引用。這是正確的,當我嘗試打電話給在scrapePages功能 – clifgray

+1

BTW的soupify_url功能,使用'global'關鍵字的最高模塊範圍也什麼都沒有。 – geoffspear

回答

5

將主代碼:

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

你定義soupify_url,即點之後。文件的底部。

Python是讀取DEF scrapePages()被定義,則它試圖調用它; scrapePages()要調用一個名爲soupify_url()功能還未被這樣定義你得到一個:

NameError: global name 'soupify_url' is not defined 

牢記規則:所有職能都必須,做實際工作的任何代碼之前被定義

如果您移動主代碼調用scrapePages()soupify_url()一切的定義將被定義後,範圍,應該可以解決您的錯誤。

+0

ah jeez,出於某種原因,我認爲Python解釋器事先掃描了代碼並發現了這個問題。感謝您修復我的錯誤假設。 – clifgray

+0

@clifgray - 快樂幫,是的,我認爲太回來時,我開始使用Python,錯誤似乎對我很熟悉。 :) – Mike

相關問題