download images from url and save as zip file in Python [closed]


download images from url and save as zip file in Python [closed]



I am getting so many online links of images. How I can now download them as a zip file?



Until now I didn't get a working solution and tried a solution following answers to this question.



So do I have to move to javascript or will it work in python?



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Can you show your code?
– Maikflow
Jul 2 at 19:35





@Maikflow I am check all codes of link given on question
– Pro-Web
Jul 2 at 19:40





They don't mention anything about downloading images/content. It's a fairly general question you've linked. What specifically about that question hasn't worked for you?
– C.Nivs
Jul 2 at 19:43





@C.Nivs I want diff thing then this question . Like want to download a online image as a zip file just.
– Pro-Web
Jul 2 at 19:46





streaming from requests will download an image as a bytes object that you can put in a zip file. The implementation in your linked question works just as well. You may want to revise your question to include what you want to do with your now-downloaded image
– C.Nivs
Jul 2 at 19:51


requests




2 Answers
2



I've had some success using the requests module to download zip files/download files to a zipped folder:


requests


# Download file contents via GET call
r = requests.get('http://myurl.com')

# Open folder and write contents to it
with open('/path/to/file.zip', 'wb') as fh:
fh.write(r.content)



This will write the downloaded bytes to the folder. This does take some time, depending on the size of the images/zip-folder being downloaded over http. Sometimes you may need to take advantage of the stream=True arg in the requests.get call if the file is particularly large


stream=True


requests.get





I want to download this as a zip file
– Pro-Web
Jul 2 at 19:40





Does it not behave as you expect? Have you run it?
– C.Nivs
Jul 2 at 19:42





yes Its not . I want a zip file download in browser .
– Pro-Web
Jul 2 at 19:45





In view . Django
– Pro-Web
Jul 2 at 19:45





You may need to flesh out your question a bit more to include your use-case. If you are attempting to display content via Django, then you need to link it as such. What I've put above downloads content from a page, and writes it to a zipped folder, per your question. If you want it to display in Django, you may need to stream the content to a form/html template. Simply asking ` how I can now download them as zip ` seems to not encapsulate your problem
– C.Nivs
Jul 2 at 19:48



I used PyCharm for this, it should work in all different IDE's I think. This code creates the ZIP files for all the images from the website, you can change the link accordingly to download pictures from specific links.


import random
import urllib.request

def download_web_image(url):
nameOfPicture = random.randrange(1, 10)
whole_name = str(nameOfPicture) + ".zip"
urllib.request.urlretrieve(url, whole_name)

download_web_image(")



However, this will only download in the folder that you have the IDE in(EX : This file will be saved within my PycharmProjects folder. )

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages