0
我仍在使用我的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)的重要原因。在Python REPL中運行你的函數,當它崩潰時,輸入'import pdb; pdb.pm()'在代碼崩潰時獲得調試器提示符。從那裏你可以直接查看像'urlretrieve'這樣的函數實際上是否返回。這應該讓你瞭解爲什麼你要用返回值做的各種事情都失敗了。 – Iguananaut