批量下载webp图片转为jpg
import requests
import re
import threading
import time
from PIL import Image
from io import BytesIO
def dis(iter):
for it in iter:
print(it)
def get(pho_url,i):
html=requests.get(pho_url)
byte_stream = BytesIO(html.content)
im = Image.open(byte_stream)
if im.mode == "RGBA":
im.load()
background = Image.new("RGB", im.size, (255, 255, 255))
background.paste(im, mask=im.split()[3])
im.save(f'2/{i}.jpg', 'JPEG')
im.close()
url='https://stock.tuchong.com/topic?topicId=50045&from=Adobe%E7%8B%AC%E5%AE%B6'
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
html=requests.get(url)
k=r'"image_id":"(?P<id>.*?)"'
ids=re.findall(k,html.text,re.S)
base_url='https://cdn6-banquan.ituchong.com/weili/smh/{}.webp'
urls=[]
for pos,pho_url in enumerate(ids):
urls.append(base_url.format(pho_url))
if (pos+1)%5==0 or len(ids)==pos+1:
thread=[]
for i in range(5):
try:
thread.append(threading.Thread(target=get,args=(urls[i],str((pos+1)/5*5-4+i),)))
except:
break
for i,it in enumerate(thread):
it.start()
print('正在下载:',str((pos+1)/5*5-4+i)+'.jpg')
for it in thread:
it.join()
urls.clear()