下載圖像我試圖下載使用該代碼的圖像:如何使用的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))。基於Python發送的內容,它拒絕訪問。我不知道如何使用urlretrieve解決這個問題。 [curl](http://curl.haxx.se/)可能可以做你想做的事,儘管它不是最好的解決方案。 – FakeRainBrigand