下载图片 保存至本地 返回路径
import requests
import time
import threading
img_url = 'https://s3.pstatp.com/toutiao/static/img/logo.201f80d.png'
img_dir = 'C:\\Users\\sas\\PycharmProjects\\py_win_to_unix\\crontab_chk_url\\personas\\trunk\\plugins\\spider\\dl_img_tmp\\'
def spider_webimg_dl_return_local_img_path(img_dir, img_url, local_default='default.DONOT_REMOVE.png'):
r = '%s%s' % (img_dir, local_default)
# http://tpc.googlesyndication.com/daca_images/simgad/14585742322992183828?w=400&h=209
if '.' not in img_url:
return r
# http://cms-bucket.nosdn.127.net/ef46d015659f49c29bc337d081a3583f20180107130343.jpeg?imageView&thumbnail=550x0
#https://img.alicdn.com/imgextra/i2/11236204/TB2tiTYoBNkpuFjy0FaXXbRCVXa_!!11236204.jpg
img_url = img_url.split('?')[0]
try:
bytes = requests.get(img_url)._content
if bytes != 0:
r = '%s%s%s%s' % (
img_dir, time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())), str(threading.get_ident()),
img_url.replace('/', '_xl_').replace(':', '_fxl_'))
with open(r, 'wb')as f:
f.write(bytes)
except Exception as e:
print(e)
return r
r = spider_webimg_dl_return_local_img_path(img_dir, img_url, local_default='default.DONOT_REMOVE.png')