2012-12-08 7 views
30

という名前のモジュールが、私はそれを修正するためにどのようにtest.pyインポートhttplibはImportError:実行したときにhttplib

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 

このエラーを得ていませんか?

コードブロック:

#!/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

あなたは、Python 3でのPython 3上のPython 2のコードを実行している、モジュールはhttp.clientに名前が変更されました。

コードに2to3 toolを実行し、自動的に翻訳しようとすることができます。 httplibへの参照は自動的に書き換えられ、代わりにhttp.clientが使用されます。

+0

私は同じ問題があるので、2to3ツールは外部ライブラリ用にも動作しますが、セレンのpythonラッパーは間違った名前を使用しているようです(最新のセレンのpythonラッパーです) –

+1

Selenium is完全にPython 3と互換性があります。 '2to3'ツールを使用してmuckingを開始する必要はありません。他の何かが間違っている可能性があります。 –