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