0

I have a lot of URLs which I need to download and save the image file as a local file. When I use my browser I can download the Image but in my code I get a ConnectionError.

The problem is when I use a URL of an Image from the web my code works correctly:

import wget
import requests
local_file = open('local_file.png','wb')
url = "https://ws.addons.om.prod/subom/mg/prod/contracts/contracts1082/C17510266/401032029940E66_photo_type_identity_front_1613034513765.jpg"
resp = requests.get(url, stream=True,verify=True ,timeout=60)
local_file.write(resp.content)
local_file.close()   
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

Make sure your link is accurate and does not have any errors, which seems to be the issue. You can shorten your code by doing this:

import requests
f = open('00000001.jpg','wb')
f.write(requests.get('http://www.gunnerkrigg.com//comics/00000001.jpg').content)
f.close()
PreciXon
  • 443
  • 2
  • 9
1

You have many urls, you need to look for threads(i.e ThreadPoolExecutor) so can download them concurrently. flow would be:

  1. get your URLs in a list say images_on_a_page
  2. create a function that will download the images, for example:
def download_image(img_url):
    data_folder=folder_where_need_to_save_images
    try:
        res=requests.get(img_url,allow_redirects = True,headers=headers)
        img_bytes= requests.get(img_url).content # download bytes for a image
        with open(os.path.join(data_folder,img_name),"wb") as img_file:
            img_file.write(img_bytes)
    except Exception as e:
        print(e)
  1. put the list and function in a ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor() as executor:
        executor.map(download_image,images_on_a_page)

I did something like this for downloading images from flicker. try the complete program , execution step 1 to 7(on notebook).

simpleApp
  • 2,885
  • 2
  • 10
  • 19