#! G:\python\python3.6\python.exe
import json
import os
import random
import hashlib
import traceback
import requests
from PIL import Image
import sqlite3
from loguru import logger

# 初始化数据库
def initSql(hash, filename):
    conn = sqlite3.connect("res.db")
    logger.info("connected to db -")
    cursor = conn.cursor()
    # 查询数据库是否存在hash
    cursor.execute("SELECT hash FROM test3 where hash=?;", (hash, ))
    v = cursor.fetchone()
    logger.info(v)
    # 如果存在数据
    if v:
        logger.info("hash exist in db, ret")
        return False
    # 涉及io操作,try一下, 将数据插入数据库
    try:
        cursor.execute("INSERT INTO test3(id, hash, filename)\
        VALUES (null, ?, ?)", (hash, filename))
    except Exception:
        traceback.print_exc()
        conn.commit()
        cursor.close()
        conn.close()
        return False
    # 提交数据
    conn.commit()
    cursor.close()
    conn.close()
    return True


# 生成文件md5,方便对比文件 信息
def getHash(filename:str) -> str:
    md5 = hashlib.md5()
    # 如果存在该文件
    if os.path.isfile(filename):
        with open(filename, 'rb') as f:
            for line in f:
                md5.update(line)
    else:
        md5.update(filename.encode())
    v = md5.hexdigest()
    logger.info(v)
    return v


def getImg():
    # 爬取图片,
    crapyUrl = ""
    r = requests.get(url=crapyUrl)
    resp = json.loads(r.content)
    rand = random.randint(0, len(resp.get("data")))

    retUrl = resp.get("data", [i for i in range(100)])[rand].get("url", "")
    logger.info(retUrl)
    # 如果存在url
    if retUrl:
        rr = requests.get(url=retUrl)
        filename = retUrl.split('/')[-1]
        # print(filename)
        # 返回文件名
        with open(filename, "wb")as fp:
            fp.write(rr.content)
    return filename


# 压缩图片,
def compressImg(filename):
    img = Image.open(filename)
    # 获取图片原来尺寸
    img_height = img.size[1]
    img_width = img.size[0]
    # 将图片等比例缩放
    img.thumbnail((int(0.2 * img_width), int(0.2 * img_height)),Image.ANTIALIAS)
    thumbnail_filename = "thumbnail_" + filename
    # 保存图片
    img.save(thumbnail_filename)


# 删除原图
def deleteImg(filename):
    os.remove(filename)


# 输入生成几张缩略图
num = int(input("input:"))
for i in range(num):
    filename = getImg()
    # 获取爬取下来图 的hash
    hash = getHash(str(filename))
    # hash传入initSql
    ret = initSql(hash, filename)
    # 如果数据库没有
    if ret:
        compressImg(filename)
        # 删除原图
        deleteImg(filename)

# 结束
input("finished-----")

接口的话要自己找了

站点统计

  • 文章总数:315 篇
  • 分类总数:20 个
  • 标签总数:193 个
  • 运行天数:1126 天
  • 访问总数:10924 人次

浙公网安备33011302000604

辽ICP备20003309号