首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入httplib ImportError:没有名为httplib的模块

导入httplib ImportError:没有名为httplib的模块
EN

Stack Overflow用户
提问于 2012-12-08 14:13:44
回答 4查看 211.7K关注 0票数 79

我在运行test.py时得到了这个错误

代码语言:javascript
复制
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

怎么改正呢?

test.py的代码块

代码语言:javascript
复制
#!/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()
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-08 14:16:21

在Python3上运行Python2代码,在Python3中,模块被重命名为http.client

您可以尝试在代码上运行 tool,并尝试将其自动翻译。对httplib的引用将自动重写为使用http.client

票数 153
EN

Stack Overflow用户

发布于 2021-05-15 20:28:14

您只需导入http.client并用以下代码将其重命名为httplib:

将http.client导入为httplib

票数 7
EN

Stack Overflow用户

发布于 2019-12-30 12:58:35

如果您使用PyCharm,请将“项目解释器”更改为“2.7.x”

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13778252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档