반응형
가져오기 httplib 가져오기 오류: 이름이 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
어떻게 수정합니까?
테스트를 위한 코드 블록입니다.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()
Python 3에서 Python 2 코드를 실행하고 있습니다.Python 3에서는 모듈 이름이 로 변경되었습니다.
코드에서 도구를 실행하고 자동으로 번역할 수 있습니다.에 대한 참조httplib
사용하도록 자동으로 다시 작성됩니다.http.client
대신.
http.client를 가져와서 다음 코드로 이름을 httplib로 변경하면 됩니다.
http.client를 httplib로 가져오기
PyCharm을 사용하는 경우 'Project Interpreter'를 '2.7.x'로 변경하십시오.
도커 컨테이너를 더 작게 만들려고 할 때 이 문제가 발생했습니다.Python 2.7에 다음을 설치했기 때문입니다.
apt-get install -y --no-install-recommends python
그리고 나는 그것을 포함하지 말았어야 했습니다.--no-install-recommends
플래그:
apt-get install -y python
언급URL : https://stackoverflow.com/questions/13778252/import-httplib-importerror-no-module-named-httplib
반응형
'programing' 카테고리의 다른 글
병합 후 커밋 시 Git 오류 - 치명적: 병합 중에 부분 커밋을 수행할 수 없습니다. (0) | 2023.05.27 |
---|---|
전체 데이터 열에서 선행 또는 후행 공백 제거 (0) | 2023.05.27 |
Git 브랜치를 안전하게 마스터에 병합하려면 어떻게 해야 합니까? (0) | 2023.05.27 |
정적 클래스 인스턴스는 ASP.NET의 요청 또는 서버에 고유합니까? (0) | 2023.05.27 |
excel에서 시간:분:초를 총 분으로 변환 (0) | 2023.05.27 |