2

我一直在构建一个小的网络爬虫,我觉得我的变量或函数范围做错了。每当我尝试将某些功能提取到单独的函数中时,它都会给我 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) 

这是我的引用:

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
4

1 回答 1

5

移动你的主要代码:

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

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

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

NameError: global name 'soupify_url' is not defined

牢记规则:必须在任何真正起作用的代码之前定义所有函数

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

于 2013-02-07T13:12:50.473 回答