Python 自动下载文件源码

自动下载服务器各目录文件

import threading
import requests
import re,os
# new_content = r.content.decode('utf-8')
print('-----第一次-----')
#第一次调用生成子目录
def request_dirname(url,pattern):
    path_new = []
    r = requests.get(url)
    new = re.findall(pattern, str(r.content))
    i = 0
    while i < len(new):
        aa = os.path.join(url, new[i])
        # print(aa)
        path_new.append(aa)
        i = i + 1
    return path_new

print('-----第二次-----')
#第二次调用生产文件目录
def request_filename(url, pattern):
    # dict_new = {}
    # num = 0
    file_compare_list = []
    path_url = request_dirname(url, pattern)
    for item in path_url:
        print("---第二次--first---")
        path_fileurl = request_dirname(item,pattern)
        # item_new = re.findall(r'8081/(.*?)/', item)
        # dict_new[num] = item_new #对应目录编号
        # num = num + 1
        file_compare_list.append(path_fileurl[1:])

    return file_compare_list

print('------第三次------')
#第三次循环写入text文件,文件url
def filename_urls(url, pattern):
    f_urls = []
    file_compare_list = request_filename(url, pattern)
    with open('filename_url.txt', 'w') as f_txt:
        f_txt.write('')
    for list01 in file_compare_list:
        for list02 in list01:
            f_urls.append(list02)
            with open('filename_url.txt','a') as f_txt:
                f_txt.write(list02+'\n')

    return f_urls

print('---------test---filename_urls--------')
pattern1 = r'<a href="(.*?)">'
url1 = 'http://172.17.3.162:8081/'
ABC = filename_urls(url1, pattern1)
print(ABC)
print(len(ABC))

urls_results = ABC[3:]
current_path = os.getcwd()
#第三步创建文件夹并下载
def download_files(url):
    path = url.split('/')[-2]
    filename = url.split('/')[-1]
    dir_path = os.path.join(current_path,'ICBC/',path)
    if os.path.exists(dir_path):
        print(dir_path)
    else:
        os.makedirs('ICBC/'+path)
        print('%s已创建'%path)
        # download_files(path)
# 文件夹
for xxl in urls_results:
    download_files(xxl)

#下载文件
def file01(url):
    filename = url.split('/')[-1]
    path = url.split('/')[-2]
    # print('@@@@@@@@@@@@@@@@@')
    # print(current_path)
    work_path = os.path.join(current_path,'ICBC',path)
    print(work_path)
    os.chdir(work_path)
    #查看当前文件
    print('-------test----------')
    r = requests.get(url)
    with open(filename,'wb') as f:
        f.write(r.content)
    print(os.listdir('.'))

for xxl in urls_results:
    file01(xxl)

结论:

上一篇:python游戏开发,猜数字小游戏。代码不多不要复制哦!


下一篇:Python简单实现批量下载无版权图片