SoFunction
Updated on 2024-10-30

python get network image method and organize the process in detail

This article introduces the python get network picture method and organize the process in detail, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference and learning value, you can refer to the following friends

Mode 1

Using the urllib library

import 
import os ,stat
url = "/th?id=OHR.Lidong2019_ZH-CN0761273672_1920x1080.jpg"
try:
  (url,filename="/home/baixiaoxu/desk/")
except IOError as e:
  print("IOE ERROR")
except Exception as e:
  print("Exception")

take note of:
1,Get Address,Determine if an address exists
2,Local save address,determine the existence of
3,Get the name of the image at the remote address,or change the name
"""
url = "/th?id=OHR.Lidong2019_ZH-CN0761273672_1920x1080.jpg"
file_suffix = (url)[1][-20:-1]
print(file_suffix)
"""

2, using the system library file read and write operations

import 
import os ,stat

req = (url)
file = "/home/baixiaoxu/desk/"
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0')
response = (url)
html = ()
with open(file, 'wb') as f:
   (html)

Online methods

import os
('./image/', exist_ok=True)
IMAGE_URL = "/2017/1122/"
 
def urllib_download():
  from  import urlretrieve
  urlretrieve(IMAGE_URL, './image/')   
 
def request_download():
  import requests
  r = (IMAGE_URL)
  with open('./image/', 'wb') as f:
    ()           
 
def chunk_download():
  import requests
  r = (IMAGE_URL, stream=True)  
  with open('./image/', 'wb') as f:
    for chunk in r.iter_content(chunk_size=32):
      (chunk)

Organize simple downloadable images

import urllib
from  urllib import request
import re

response = ('/')
html = ()
ht = ()
pattern = r'bgLink(.*?\.jpg)'
compile_re = (pattern)

hh = compile_re.findall(ht)
url = hh[0].split('/')[1]

download = '/' + url
(download,filename="/home/baixiaoxu/desk/")

This is the whole content of this article.