から画像をダウンロードするためにurllibは使用方法:それが働いた私はこのコードを使用してイメージをダウンロードしようとしているウェブ
from urllib import urlretrieve
urlretrieve('http://gdimitriou.eu/wp-content/uploads/2008/04/google-image-search.jpg',
'google-image-search.jpg')
。画像はダウンロードされ、任意の画像ビューアソフトウェアで開くことができます。
ただし、以下のコードは機能しません。ダウンロードした画像はわずか2KBで、どの画像ビューアでも開くことはできません。
from urllib import urlretrieve
urlretrieve('http://upload.wikimedia.org/wikipedia/en/4/44/Zindagi1976.jpg',
'Zindagi1976.jpg')
結果はHTML形式です。
ERROR
The requested URL could not be retrieved
While trying to retrieve the URL: http://upload.wikimedia.org/wikipedia/en/4/44/Zindagi1976.jpg
The following error was encountered:
Access Denied.
Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.
Your cache administrator is nobody.
Generated Mon, 05 Dec 2011 17:19:53 GMT by sq56.wikimedia.org (squid/2.7.STABLE9)
2KBは通常プレーンテキストまたはhtmlです。 Zindagi1976.jpg'''''を '' ''Zindagi1976.html''''に変更してブラウザで開きます。この情報はデバッグに役立つ場合があります。 (私はヘッダーの問題を疑う。)ここに投稿してください。 – FakeRainBrigand
@FakeRainBrigand更新を参照 – Kulbir
ウィキメディアがあなたの要求をチェックしているようです。ブラウザの画像に移動すると、あなたのセットアップに関する情報(Wikimedia.org)(例:あなたの[user-agent](http://en.wikipedia.org/wiki/User_agent))がWikimedia.orgに送信されます。これまでにPythonが送信したことに基づいて、アクセスを拒否しています。私はurlretrieveを使ってこれを修正する方法を知らない。 [カール](http://curl.haxx.se/)はおそらくあなたが望むことをすることができますが、それは最も良い解決策ではありません。 – FakeRainBrigand