文字列に格納された URL を使用して Python 3 で Web からファイルをダウンロードする方法
Python 3 の Web サーバーで URL として文字列を渡すと、バイト入力を要求するエラーが発生する可能性があります。この記事では、この問題を回避してファイルを正常に取得するためのいくつかの方法を紹介します。
urllib.request.urlopen の使用
Web ページのコンテンツを取得するには、urllib を利用します。 .request.urlopen()。この関数は応答オブジェクトを返します。このオブジェクトは変数に読み込むことができます。
<code class="python">import urllib.request url = 'http://example.com/' response = urllib.request.urlopen(url) data = response.read() # a `bytes` object</code>
urlib.request.urlretrieve を使用する
ダウンロードとファイルを保存するには、urllib.request.urlretrieve 関数を利用します。
<code class="python">import urllib.request url = 'http://example.com/file.txt' urllib.request.urlretrieve(url, 'file.txt')</code>
このメソッドは、指定された URL からファイルをダウンロードし、'file.txt' としてローカルに保存します。
shutil.copyfileobj での urlib.request.urlopen の使用
ダウンロード プロセスをより詳細に制御するには、urllib.request.urlopen() を使用してファイルのようなオブジェクトを返します。このオブジェクトは、shutil.copyfileobj() を使用して実際のファイルにコピーできます。
<code class="python">import urllib.request import shutil url = 'http://example.com/file.zip' with urllib.request.urlopen(url) as response, open('file.zip', 'wb') as out_file: shutil.copyfileobj(response, out_file)</code>
ダウンロードをバイトとして保存
速度を優先する場合は、ダウンロードされたデータはバイト オブジェクトに直接保存し、その後ファイルに書き込むことができますが、これは小さいファイルにのみ適しています:
<code class="python">import urllib.request url = 'http://example.com/icon.png' with urllib.request.urlopen(url) as response, open('icon.png', 'wb') as out_file: data = response.read() # a `bytes` object out_file.write(data)</code>
圧縮ファイルの処理
サーバーがランダム アクセスをサポートしている場合、urllib.request.urlopen() を使用して圧縮ファイルを処理することもできます。
<code class="python">import urllib.request import gzip url = 'http://example.com/archive.gz' with urllib.request.urlopen(url) as response: with gzip.GzipFile(fileobj=response) as uncompressed: file_header = uncompressed.read(64) # a `bytes` object</code>
これらのメソッドのいずれかを実装することで、開発者は Python 3 で Web からファイルを正常にダウンロードできます。 URL が文字列として保存されている場合でも。
以上がPython 3で文字列として保存されたURLからファイルをダウンロードするにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Dreamweaver Mac版
ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ホットトピック









