2012-12-08 1 views
30

나는이 오류가있어 이름이 없다?수입 HTTPLIB의 ImportError를이 : 그것을 해결하는 방법 <pre><code>C:Python32>python.exe test.py Traceback (most recent call last): File "test.py", line 5, in <module> import httplib ImportError: No module named httplib </code></pre> <p>test.py</p>를 실행할 때 모듈은 HTTPLIB

코드 블록은 :

#!/usr/local/bin/python 

import httplib 
import sys 
import re 
from HTMLParser import HTMLParser 


class miniHTMLParser(HTMLParser): 

    viewedQueue = [] 
    instQueue = [] 

    def get_next_link(self): 
    if self.instQueue == []: 
     return '' 
    else: 
     return self.instQueue.pop(0) 


    def gethtmlfile(self, site, page): 
    try: 
     httpconn = httplib.HTTPConnection(site) 
     httpconn.request("GET", page) 
     resp = httpconn.getresponse() 
     resppage = resp.read() 
    except: 
     resppage = "" 

    return resppage 


    def handle_starttag(self, tag, attrs): 
    if tag == 'a': 
     newstr = str(attrs[0][1]) 
     if re.search('http', newstr) == None: 
     if re.search('mailto', newstr) == None: 
      if re.search('htm', newstr) != None: 
      if (newstr in self.viewedQueue) == False: 
       print (" adding", newstr) 
       self.instQueue.append(newstr) 
       self.viewedQueue.append(newstr) 
      else: 
      print (" ignoring", newstr) 
     else: 
      print (" ignoring", newstr) 
     else: 
     print (" ignoring", newstr) 


def main(): 

    if sys.argv[1] == '': 
    print ("usage is ./minispider.py site link") 
    sys.exit(2) 

    mySpider = miniHTMLParser() 

    link = sys.argv[2] 

    while link != '': 

    print ("\nChecking link ", link) 

    # Get the file from the site and link 
    retfile = mySpider.gethtmlfile(sys.argv[1], link) 

    # Feed the file into the HTML parser 
    mySpider.feed(retfile) 

    # Search the retfile here 

    # Get the next link in level traversal order 
    link = mySpider.get_next_link() 

    mySpider.close() 

    print ("\ndone\n") 

if __name__ == "__main__": 
    main() 

답변

54

당신은 파이썬 3에서 파이썬 3에 파이썬 2 코드를 실행하는 모듈은 http.client로 이름이 바뀌 었습니다.

코드에 2to3 tool을 실행하고 자동으로 번역하려고 할 수 있습니다. httplib에 대한 참조는 자동으로 http.client을 사용하도록 다시 작성됩니다.

+0

2to3 도구는 셀레늄으로 외부 lib에도 사용할 수 있습니다. 셀레늄 파이썬 래퍼가 잘못된 이름을 사용하고있는 것으로 보입니다. (업데이트하므로 마지막 셀레늄 파이썬 래퍼가됩니다) –

+1

셀레늄은 완전히 파이썬 3 호환. '2to3' 도구로 작업을 시작할 필요가 없어야합니다. 대체로 뭔가 잘못된 것 같습니다. –

관련 문제