计算文件的MD5值

import requests
import hashlib
import os
import time

def get_file_md5(file_path):
    if os.path.isfile(file_path):
        with open(file_path, 'rb') as f:
            content = f.read()
        md5_obj = hashlib.md5()
        md5_obj.update(str(content).encode(encoding='utf8'))
        hash_code = md5_obj.hexdigest()
        md5 = str(hash_code).lower()
        return md5
    else:
        return None

def crawler_func():
    base_url = 'http://www.baidu.com'
    res = requests.get(base_url)
    if res.status_code == requests.codes.ok:
        print('请求成功')
        file_path = os.path.join(os.path.dirname(os.path.abspath('__file__')), 'index.html')
        with open(file_path, 'w', encoding='utf8') as f:
            f.write(res.text)
        md5_value = get_file_md5(file_path)
        if md5_value:
            print('该文件md5值为{}', md5_value)
        else:
            print('文件md5值计算失败')

if __name__ == "__main__":
    crawler_func()
    time.sleep(3)
    crawler_func()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容