Python 之批量自动下载FDFS现网指定资源,导入测试环境

1. fdfs_getinfor_mysql.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import MySQLdb
import re


def getinfor_mysql():
    # 打开数据库连接(连接测试环境数据库)
    db = MySQLdb.connect("x.x.x.x","user","pwd","db")

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # SQL语句列表,查询FDFS数据库单个字段
    Sql_List = [
        "SELECT trunk_pic FROM t_ee_select WHERE trunk_pic != ''",
        "SELECT trunk_pic FROM t_ee_fill WHERE trunk_pic != ''"
    ]

    Group1_Files = []
    Group2_Files = []
    for Sql_Select in Sql_List:
        # 使用 execute 方法执行SQL语句
        cursor.execute(Sql_Select)
        # 使用 fetchall() 方法获取全部数据
        data = cursor.fetchall()
        for File_Url in data:
            if re.match(r'group1',File_Url[0]):
                Group1_Files.append(File_Url[0])
            elif re.match(r'group2',File_Url[0]):
                Group2_Files.append(File_Url[0])
            else:
                pass
    # 关闭数据库连接
    db.close()

    return Group1_Files,Group2_Files

if __name__ == "__main__":
    File1_List,File2_List = getinfor_mysql()
    print(len(File1_List))
    print("==== 分割符 ====")
    print(len(File2_List))

2.  fdfs_get_files.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
import os
import time
import re
from fdfs_getinfor_mysql import getinfor_mysql


def get_files(File_Url):
    count = 0
    for i in File_Url:
     
        File_Path = re.sub("/[^/]*..*$","",i)
        filename = re.search("[^/]*..*$",i).group()
        path = os.getcwd()
        Full_path = path + "\fdfs_files\" + File_Path.replace('/','\')
if os.path.isdir(Full_path): pass else: os.makedirs(Full_path) # URL: https://www.test.com:443/resourceProxy/fdfsDownload/group1/M00/00/49/oYYBAFhjV5GEGiBtAAAAAHqQs4o954.mp3 URL = "https://www.test.com:443/resourceProxy/fdfsDownload/" + i Full_File_Path = Full_path + '\' + filename if os.path.isfile(Full_File_Path): pass else: Web_File = open(Full_File_Path,"wb+") Get_Web = requests.get(URL) Web_File.write(Get_Web.content) Web_File.close() count += 1 print(count) #time.sleep(0.1) #print(Full_path) #print(filename) #print(URL) #print(Full_File_Path) if __name__ == "__main__": Group1_Files,Group2_Files = getinfor_mysql() #Group1_Files = ["group1/M00/00/6D/ooYBAFkRe5KEV-vfAAAAAC5de5I654.mp3","group2/M00/00/73/pIYBAFeZYCWED_AhAAAAACw_MSg101.jpg"] get_files(Group1_Files) get_files(Group2_Files)
原文地址:https://www.cnblogs.com/52python/p/7119566.html