2013-12-15 90 views
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() 

回答

2

重讀the documentation for urllib.urlretrieve

返回一個元組(文件名,標題)其中filename是本地文件 名下該對象可以發現,和頭是無論 信息( )由urlopen()返回的對象的方法返回(對於可能緩存的 遠程對象)。

您似乎期待它返回文件本身的字節。 urlretrieve這一點是它爲你處理寫入文件,並返回它寫入的文件名(如果你提供了一個函數,它通常與你函數的第二個參數是一樣的)。

+2

順便說一下,這種事情是學習使用[pdb](http://docs.python.org/2/library/pdb.html)的重要原因。在Python REPL中運行你的函數,當它崩潰時,輸入'import pdb; pdb.pm()'在代碼崩潰時獲得調試器提示符。從那裏你可以直接查看像'urlretrieve'這樣的函數實際上是否返回。這應該讓你瞭解爲什麼你要用返回值做的各種事情都失敗了。 – Iguananaut

相關問題