Reputation: 2265
I have simple django project and I want to download image from website to my media. Url looks like this:
https://i.ytimg.com/vi/Ks-_Mh1QhMc/hqdefault.jpg
How can i do this using python 3.x?
Upvotes: 1
Views: 8301
Reputation: 11938
By far the simplest and fastest way to download one or more public accessible files with an extension (here it is JPG but could be DOC or PDF etc.) is curl
.
It should be available cross-platforms and easy to OSshell.
For a single file we just default its own name and it is down faster than one second:
curl -O https://i.ytimg.com/vi/Ks-_Mh1QhMc/hqdefault.jpg
It will retain the remote name as here hqdefault.jpg
.
However if the name is missing or embedded partway in the URL we need to assign a name using lowercase -o
rather than Uppercase -O
for example:
curl -o Matrix.png https://vignette2.wikia.nocookie.net/matrix/images/8/84/Matrix.png/revision/latest?cb=20110307094037
We can thus simply provide a list of scrapped URLS in a list of mix and match with curl
prefix and later run as a shell script. Here in Windows:
Upvotes: 0
Reputation: 5703
use urllib.request
import urllib.request as req
imgurl ="https://i.ytimg.com/vi/Ks-_Mh1QhMc/hqdefault.jpg"
req.urlretrieve(imgurl, "image_name.jpg")
Upvotes: 6