次のコードを使用して、インターネットからローカルファイルに大きなファイルをストリーミングします。
fp = open(file, 'wb')
req = urllib2.urlopen(url)
for line in req:
fp.write(line)
fp.close()
これは機能しますが、ダウンロードが非常に遅くなります。より速い方法はありますか?(ファイルは大きいので、メモリに保持したくありません。)
行ごとに作業する理由はありません (小さなチャンクであり、行末を見つけるには Python が必要です!-)。大きなチャンクに分割するだけです。
# from urllib2 import urlopen # Python 2
from urllib.request import urlopen # Python 3
response = urlopen(url)
CHUNK = 16 * 1024
with open(file, 'wb') as f:
while True:
chunk = response.read(CHUNK)
if not chunk:
break
f.write(chunk)
さまざまな CHUNK サイズを少し試して、要件に合った「スイート スポット」を見つけてください。
shutilを使用することもできます:
import shutil
try:
from urllib.request import urlopen # Python 3
except ImportError:
from urllib2 import urlopen # Python 2
def get_large_file(url, file, length=16*1024):
req = urlopen(url)
with open(file, 'wb') as fp:
shutil.copyfileobj(req, fp, length)
mechanize以前はモジュールとそのBrowser.retrieve()メソッドを使用していました。以前は100%のCPUを使用し、ダウンロードが非常に遅くなりましたが、最近のリリースではこのバグが修正され、非常に高速に動作します。
例:
import mechanize
browser = mechanize.Browser()
browser.retrieve('http://www.kernel.org/pub/linux/kernel/v2.6/testing/linux-2.6.32-rc1.tar.bz2', 'Downloads/my-new-kernel.tar.bz2')
Mechanizeはurllib2に基づいているので、urllib2にも同様のメソッドを使用できます...しかし、現在は見つかりません。
urllib.retrieve() を使用してファイルをダウンロードできます。
例:
try:
from urllib import urlretrieve # Python 2
except ImportError:
from urllib.request import urlretrieve # Python 3
url = "http://www.examplesite.com/myfile"
urlretrieve(url,"./local_file")