84

I got this error when run test.py

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

How to correct it?

Code block for test.py:

#!/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()
Elshan
  • 7,339
  • 4
  • 71
  • 106

4 Answers4

156

You are running Python 2 code on Python 3. In Python 3, the module has been renamed to http.client.

You could try to run the 2to3 tool on your code, and try to have it translated automatically. References to httplib will automatically be rewritten to use http.client instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • why do i get this error when pip install http.client? `Could not find a version that satisfies the requirement http.client (from versions: ) No matching distribution found for http.client ` – Lei Yang Sep 04 '19 at 03:07
  • 1
    @LeiYang you can’t install the library, it is part of the standard library of Python 3. If you are using Python 2 and get an import error for `http.client` you need to use Python 3 instead. – Martijn Pieters Sep 04 '19 at 10:59
14

you can just import http.client and rename it to httplib with this code :

import http.client as httplib

hassan tarif
  • 258
  • 3
  • 6
-1

If you use PyCharm, please change you 'Project Interpreter' to '2.7.x'

enter image description here

Yu Zhang
  • 1,202
  • 1
  • 11
  • 16
-2

I had this issue when I was trying to make my Docker container smaller. It was because I'd installed Python 2.7 with:

apt-get install -y --no-install-recommends python

And I should not have included the --no-install-recommends flag:

apt-get install -y python
Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108