2013-10-06 2 views
-5

나는 searchengine.py 파일을 가지고 있으며 그에 대한 색인도 만들었습니다.파이썬에서 전역 이름 오류

searchengine.py :

import sqlite3 
import urllib2 
from bs4 import BeautifulSoup 
from urlparse import urljoin 

# Create a list of words to igonre 
ignorewords=set(['the','of','to','and','a','in','is','it']) 

class crawler: 
    # Initialize the crawler with the name of database 
    def __init__(self,dbname): 
     self.con=sqlite3.connect(dbname) 

    def __del__(self): 
     self.con.close() 

    def dbcommit(self): 
     pass 

    # Auxilliary function for getting an entry id and 
    # adding it if not present 
    def getentryid(self, table, field, value, createnew=True): 
     cur=self.con.execute("select rowid from %s where %s='%s'" % (table,field,value)) 
     res=cur.fetchone() 
     if res==None: 
      cur=self.con.execute("insert into %s (%s) values ('%s')" % (table,field,value)) 
      return cur.lastrowid 
     else: 
      return res[0] 

    # Index an individual page 
    def addtoindex(self,url,soup): 
     if self.isindexed(url): return 
     print 'Indexing %s' %url 

     # Get the individual words 
     text=self.gettextonly(soup) 
     words=self.separatewords(text) 

     # Get the URL id 
     urlid=self.getentryid('urllist','url',url) 

     # Link each word to this url 
     for i in range(len(words)): 
      word=words[i] 
      if word in ignorewords: continue 
      wordid=self.getentryid('wordlist','word',word) 
      self.con.execute("insert into wordlocation(urlid,wordid,location) \ 
       values (%d,%d,%d)" % (urlid,wordid,i)) 


    # Extract the text from an HTML page (no tags) 
    def gettextonly(self,soup): 
     v=soup.string 
     if v==None: 
      c=soup.contents 
      resulttext='' 
      for t in c: 
       subtext=self.gettextonly(t) 
       resulttext+=subtext+'\n' 
      return resulttext 
     else: 
      return v.strip() 


    # Sepetate the words by any non-whitespace character 
    def separatewords(self, text): 
     splitter=re.compile('\\W*') 
     return [s.lower() for s in splitter.split(text) if s!=''] 

    # Return true if this url is already indexed 
    def isindexed(self, url): 
     u=self.con.execute("select rowid from urllist where url='%s'" % url).fetchone() 
     if u!=None: 
      # Check if it has actually been crawled 
      v=self.con.execute('select * from wordlocation where urlid=%d' % u[0]).fetchone() 
      if v!=None: return True 
     return False 

    # Add a link between two pages 
    def addlinkref(self,urlFrom,urlTo,linkText): 
     pass 

    # Starting with a list of pages, do a breadth first search to 
    # the given depth, indexing pages as we go 
    def crawl(self,pages,depth=2): 
     pass 

    # Create the database tables 
    def createindextables(self): 
     pass 

    def crawl(self,pages,depth=2): 
     for i in range(depth): 
      newpages=set() 
      for page in pages: 
       try: 
        c=urllib2.urlopen(page) 
       except: 
        print "Could not open %s" % page 
        continue 
       soup=BeautifulSoup(c.read()) 
       self.addtoindex(page,soup) 

       links=soup('a') 
       for link in links: 
        if ('href' in dict(link.attrs)): 
         url=urljoin(page,link['href']) 
         if url.find("'")!=-1: continue 
         url=url.split('#')[0] # remove location portion 
         if url[0:4]=='http' and not self.isindexed(url): 
          newpages.add(url) 
         linkText=self.gettextonly(link) 
         self.addlinkref(page,url,linkText) 

       self.dbcommit() 

      pages=newpages 

    # Creating index tables 
    def createindextables(self): 
     self.con.execute('create table urllist(url)') 
     self.con.execute('create table wordlist(word)') 
     self.con.execute('create table wordlocation(urlid,wordid,location)') 
     self.con.execute('create table link(fromid integer,toid integer)') 
     self.con.execute('create table linkwords(wordid,linkid)') 
     self.con.execute('create index wordid on wordlist(word)') 
     self.con.execute('create index urlid on urllist(url)') 
     self.con.execute('create index wordurlidx on wordlocation(wordid)') 
     self.con.execute('create index urltoidx on link(toid)') 
     self.con.execute('create index urlfromidx on link(fromid)') 
     self.dbcommit() 

만든 인덱스 -이처럼 사용하여 시도했지만 오류가 제기되고

>>> reload(searchengine) 
>>> crawler=searchengine.crawler('searchindex.db') 
>>> crawler.createindextables() 

searchindex.db 사용하여 파이썬 쉘 :

>>> reload(searchengine) 
>>> crawler=searchengine.crawler('searchindex.db') 
>>> pages=['http://kiwitobes.co/wiki/Categorical_list_of_programming_languages.html'] 
>>> crawler.crawl(pages) 
Indexing http://www.tartarus.org/~martin/PorterStemmer/index.html 

Traceback (most recent call last): 
    File "<pyshell#22>", line 1, in <module> 
    crawler.crawl(pages) 
    File "C:/Users/dj/Desktop\searchengine.py", line 103, in crawl 
    self.addtoindex(page,soup) 
    File ""C:/Users/dj/Desktop\searchengine.py", line 38, in addtoindex 
    words=self.separatewords(text) 
    File ""C:/Users/dj/Desktop\searchengine.py", line 68, in separatewords 
    splitter=re.compile('\\W*') 
NameError: global name 're' is not defined 

파이썬 버전 : 2.7, 운영체제 : Windows 8

+0

'수입 재발행 '이 없습니다. – falsetru

답변

0

당신은 당신의 코드에서 re 모듈을 사용

def separatewords(self, text): 
    splitter=re.compile('\\W*') 
    # here --^ 
    return [s.lower() for s in splitter.split(text) if s!=''] 

하지만 한 번 나는 당신이 볼 않았다

메모리에 re 모듈을로드
import re 

. 메모리에로드되지 않은 모듈을 사용하려고하면 NameError가 생성됩니다.

문제를 해결하려면 다른 모든 가져 오기를 사용하여 스크립트 상단에 import re을 추가하기 만하면됩니다.

+0

나는이 대답에 대담하게 답변하기 위해 downvote를 받았는데, 그 후에 OP는 의견에 새로운 역 추적을 게시했다. HTML 구문 분석 오류. 나는 제대로 대응할 시간이 없었기 때문에 지금 내 대답을 삭제했다. –

+0

@MartijnPieters - 알겠습니다. 글쎄, BeautifulSoup을 설치하지 않았기 때문에 실제로 거기서 도울 수는 없다. (아이러니하게도 나는 파이썬을 사용하면서 필요를 가로 질렀다.) 나는 그가 질문에서 주었던 문제를 해결했기 때문에 여전히 나의 지위를 유지할 것이다. 결국, SO는 여러분의 질문에 답하고 _specific_ 문제를 해결하기위한 것입니다 ... 전체 코드를 디버깅하지 않습니다. 다른 일을해야한다면 새로운 질문을해야합니다. – iCodez

관련 문제