2012-06-21 23 views

回答

1

這並不重要,你想下載種子文件。 您可以下載並保存任何類型的文件是這樣的:從http://megatorrent.com/torrent-url

import urllib2 
with open("mytorrent", "w") as f: 
    f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read()) 

文件會被保存在當前目錄mytorrent

當要保存在其他目錄下的文件,你做這樣的事情:

import urllib2 
with open(os.path.join(torrents_die_path, "mytorrent"), "w") as f : 
    f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read()) 
+1

Python文檔建議不要使用'file()'來打開一個文件 - 用'with'語句使用'open()'是最好的方法。實際上,它在3.x中被刪除 –

+0

@Latteware:很好的提示,謝謝! –

2

而不是做自己使用urllib2.urlopen的工作,我建議使用urllib.urlretrieve因爲這將做後面的工作場面。

相關問題