나는 여전히 내 MP3 다운로더에서 작업하고 있지만 현재 다운로드중인 파일에 문제가 있습니다. 나는 두 가지 버전의 부분을 가지고있어. 첫 번째 파일은 적절한 파일을 제공하지만 오류가 발생합니다. 두 번째 파일은 너무 작지만 오류가없는 파일을 제공합니다. 바이너리 모드에서 파일을 열려고 시도했지만 도움이되지 않았습니다. 나는 html로 어떤 일을하기에 꽤 새로울 것이므로 어떤 도움도 감사 할 것입니다.urllib.urlretrieve를 사용하여 HTTP를 통해 파일을 다운로드 할 수 없음
import urllib
import urllib2
def milk():
SongList = []
SongStrings = []
SongNames = []
earmilk = urllib.urlopen("http://www.earmilk.com/category/pop")
reader = earmilk.read()
#gets the position of the playlist
PlaylistPos = reader.find("var newPlaylistTracks = ")
#finds the number of songs in the playlist
NumberSongs = reader[reader.find("var newPlaylistIds = "): PlaylistPos].count(",") + 1
initPos = PlaylistPos
#goes though the playlist and records the html address and name of the song
for song in range(0, NumberSongs):
songPos = reader[initPos:].find("http:") + initPos
namePos = reader[songPos:].find("name") + songPos
namePos += reader[namePos:].find(">")
nameEndPos = reader[namePos:].find("<") + namePos
SongStrings.append(reader[songPos: reader[songPos:].find('"') + songPos])
SongNames.append(reader[namePos + 1: nameEndPos])
initPos = nameEndPos
for correction in range(0, NumberSongs):
SongStrings[correction] = SongStrings[correction].replace('\\/', "/")
#downloading songs
fileName = ''.join([a.isalnum() and a or '_' for a in SongNames[0]])
fileName = fileName.replace("_", " ") + ".mp3"
# This version writes a file that can be played but gives an error saying: "TypeError: expected a character buffer object"
## songDL = open(fileName, "wb")
## songDL.write(urllib.urlretrieve(SongStrings[0], fileName))
# This version creates the file but it cannot be played (file size is much smaller than it should be)
## url = urllib.urlretrieve(SongStrings[0], fileName)
## url = str(url)
## songDL = open(fileName, "wb")
## songDL.write(url)
songDL.close()
earmilk.close()
그런데, 이런 종류의 일은 [pdb] (http://docs.python.org/2/library/pdb.html)를 사용하는 것을 배우기 좋은 이유입니다. 파이썬 REPL에서 함수를 실행하면 충돌이 발생할 때'import pdb; pdb.pm()'을 호출하여 코드가 충돌 한 지점에서 디버거 프롬프트를 표시합니다. 거기에서'urlretrieve '와 같은 함수가 실제로 반환되는지 직접 확인할 수 있습니다. 그 이유는 반환 값을 사용하여 수행해야하는 다양한 작업이 실패하는 이유를 알 수 있어야합니다. – Iguananaut