python - NameError:全局名称 'NAME' 未定义

标签 python function web-scraping nameerror

我在构建一个小型网络爬虫期间度过了一段有趣的时光,我认为我的变量或函数范围做错了。每当我尝试将某些功能提取到单独的函数中时,都会出现 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

最佳答案

移动您的主要代码:

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() 定义之后,所有内容都将被定义并在范围内,应该可以解决您的错误。

关于python - NameError:全局名称 'NAME' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751688/

相关文章:

python - 删除重复的 Python 循环链表

python - 从 Python 中的数字中获取信号名称

c - 传递 char 的二维数组作为参数

php - 使用PHP的关联数组的一行总和?

python - 如何从 scrapy 运行中获取统计信息?

python - scipy.stats 中 cdf 的精度

python - AttributeError : 'str' object has no attribute 'write' error in last line, 需要洞察

javascript - super 简单的javascript函数调用

python - 如何从抓取的链接 [Python] 下载 PDF?

python - 使用 BeautifulSoup 直接从 HTML 中提取数据