python篇---批量下载csv文件

python篇—批量下载csv文件

用代码通过 url 批量下载 csv文件

import os
import time
import requests


def save2csv(url, csv_name):
    response = requests.get(url)
    print(response)
    with open(os.path.join(SAVE_PATH, csv_name), 'wb') as f:
        f.write(response.content)

# FA7953.txt
f_name = "xxx.txt"
SAVE_PATH = "folder/" + f_name.split(".")[0]
if not os.path.exists(SAVE_PATH):
    os.makedirs(SAVE_PATH)


file = open(f_name)
for line in file:
    print(line)
    data = line.split("/")[-1].split("_")[0]
    url = 'http://xxx.cn:8090/pls//files/{}_5_PDHisPosi.csv'.format(data)
    csv_name = url.split("/")[-1]
    response = requests.get(url)
    print(response)
    s_t =time.time()
    with open(os.path.join(SAVE_PATH, csv_name), 'wb') as f:
        f.write(response.content)
    e_t = time.time()
    print(e_t - s_t)
file.close()

我发现直接用 line 作 url会报错,需要拼接字符串

你可能感兴趣的:(python,开发语言)