SoFunction
Updated on 2024-11-18

Explaining the two ways Python downloads images and saves them locally

One: Use the urlretrieve() function in the urllib class in Python to download resources directly from the Internet to local, specific code:

import os,stat
import 
 
img_url="/timg?image&quality=80&size=b9999_10000&sec=1516371301&di=d99af0828bb301fea27c2149a7070" \
  "d44&imgtype=jpg&er=1&src=http%3A%2F%%2F2017%2F0718%"
file_path='D:/book/img'
file_name ="pyt"
 
try:
 # Is there this path
 if not (file_path):
 #Creating paths
  (file_path)
  #Get image suffixes
 file_suffix = (img_url)[1]
 print(file_suffix)
  # Splice image name (with path)
 filename = '{}{}{}{}'.format(file_path,,file_name,file_suffix)
 print(filename)
  #Download images and save them to a folder
 (img_url,filename=filename)
 
except IOError as e:
 print("IOError")
except Exception as e:
 print("Exception")

II: Write to a file using read and write operations, specific code:

import os,stat
import 
 
for i in range(1,3):
 if not ("./rym"):
  print("Impure in")
  ("./rym")
 
 else:
  print("Existence.")
  ("D:/imagss",777)
 
 
  with ("/timg?image&quality=80&size=b9999_10000&sec=1516371301&di=d99af0828b"
         "b301fea27c2149a7070d44&imgtype=jpg&er=1&src=http%3A%2F%%2F2017%2F0718%"
         "", timeout=30) as response, open("./rym/"
    , 'wb') as f_save:
   f_save.write(())
   f_save.flush()
   f_save.close()
   print("Success.")

The above is a small introduction to the Python download images and save them locally in two ways detailed integration, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. Here also thank you very much for your support of my website!