Python 2.7 error downloading Dropbox file by urllib.urlretrieve

笑着哭i 提交于 2019-12-11 13:54:33

问题


The code I used (don't mind the image, it's a test one):

import urllib
urllib.urlretrieve('https%3A//dl.dropboxusercontent.com/u/95587456/Evenimente/1.jpg','/storage/emulated/0/Temp/1.jpg')

I tried to download an image by its public dropbox link, only for this error to pop up:

I/python  ( 3750):  Traceback (most recent call last):
I/python  ( 3750):    File "/home/cristi/Desktop/AplicatieMinister/.buildozer/android/app/main.py", line 102, in <module>
"/home/cristi/Desktop/AplicatieMinister/.buildozer/android/app/main.py", line 63, in IncarcaAfise
I/python  ( 3750):    File "/home/cristi/Desktop/AplicatieMinister/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/urllib.py", line 91, in urlretrieve
I/python  ( 3750):    File "/home/cristi/Desktop/AplicatieMinister/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/urllib.py", line 237, in retrieve
I/python  ( 3750):    File "/home/cristi/Desktop/AplicatieMinister/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/urllib.py", line 205, in open
I/python  ( 3750):    File "/home/cristi/Desktop/AplicatieMinister/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/urllib.py", line 461, in open_file
I/python  ( 3750):    File "/home/cristi/Desktop/AplicatieMinister/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/urllib.py", line 475, in open_local_file
I/python  ( 3750):  IOError: [Errno 2] No such file or directory: 'https://dl.dropboxusercontent.com/u/95587456/Evenimente/1.jpg'

Running this code on Windows has no issues (it downloads the file), but on android (after compiling it as an APK by buildozer) it gives the previous error.

I tried adding ?dl=1 and ?dl=0 at the end of the link, and it ended up in the same error. Also, I tried urllib.quote on the link (and replacing the '$3A' with ':') and it still gave this error.

Is there anything I have overlooked regarding how urllib and urlretrieve work under android?

Also, if it helps, Kivy/buildozer were used in the development of this.


回答1:


Based on the stack trace, it appears that urllib.urlretrieve thinks your Dropbox URL is a local file path, as opposed to an Internet URL, and is attempting to open it as such. (I.e., it tries to use open_local_file.) That is supposed to be automatically determined based on the format of the URL, but this is apparently confused by your attempt to escape the ":", which should be unnecessary.

Your sample code actually fails the same way for me, just in the Python interpreter:

In [1]: import urllib

In [2]: urllib.urlretrieve('https%3A//dl.dropboxusercontent.com/u/95587456/Evenimente/1.jpg','./1.jpg')
...
IOError: [Errno 2] No such file or directory: 'https://dl.dropboxusercontent.com/u/95587456/Evenimente/1.jpg'

That being the case, are you sure you've run the code you thought were you on the different platforms as you mentioned?

Just fixing the URL back to normal works for me:

In [1]: import urllib

In [2]: urllib.urlretrieve('https://dl.dropboxusercontent.com/u/95587456/Evenimente/1.jpg','./1.jpg')
Out[2]: ('./1.jpg', <httplib.HTTPMessage instance at 0x10da6d878>)

(I also modified the second parameter to make it work on my computer, but that's unrelated.)



来源:https://stackoverflow.com/questions/31076587/python-2-7-error-downloading-dropbox-file-by-urllib-urlretrieve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!