重命名使用 Python 请求下载的文件 [英] Renaming the file downloaded with Python Requests

查看:40
本文介绍了重命名使用 Python 请求下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换使用 Python Requests 下载的 pdf 文件的名称?

How can I replace the name of pdf file which is downloaded with Python Requests?

我想将它保存为 Manual_name1.pdf 而不是 Elkinson%20Jeffrey.pdf

I want to save it as Manual_name1.pdf not as Elkinson%20Jeffrey.pdf

CSV 文件如下所示:

CSV file looks like:

Manual_name1 https://www.adndrc.org/diymodule/doc_panellist/Elkinson%20Jeffrey.pdf
Manual_name2 http://www.parliament.bm/uploadedFiles/Content/House_Business/Presentation_of_Papers_and_of_Reports/PCA%20Report%209262014.pdf
manual_name3 http://www.ohchr.org/Documents/HRBodies/OPCAT/elections2016/HaimoudRamdan.pdf

我当前的代码:

import os
import csv
import requests

write_path = 'C:\\Users\\hgdht\\Desktop\\Downloader_Automation'  # ASSUMING THAT FOLDER EXISTS!

with open('Links.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile)
    for link in spamreader:
        if not link:
            continue
        print('-'*72)
        pdf_file = link[0].split('/')[-1]
        with open(os.path.join(write_path, pdf_file), 'wb') as pdf:
            try:
                # Try to request PDF from URL
                print('TRYING {}...'.format(link[0]))
                a = requests.get(link[0], stream=True)
                for block in a.iter_content(512):
                    if not block:
                        break

                    pdf.write(block)
                print('OK.')
            except requests.exceptions.RequestException as e:  # This 
will catch ONLY Requests exceptions
                print('REQUESTS ERROR:')
                print(e)  # This should tell you more details about the error

推荐答案

代替

pdf_file = link[0].split('/')[-1]

使用 csv 文件中的特定列:

use the specific column from the csv file:

pdf_file = link[1]  # (assuming the file name is in the second column)

如果文件名在第一列,你应该使用

If the file name is in the first column, you should use

pdf_file = link[0]  # (assuming the file name is in the first column)
# OR
import time  # put this in the beginning of your script
pdf_file = '{}-{}.pdf'.format(link[0], int(time.time()))
# file name will look like: "name-1495460691.pdf"

但是在使用请求调用链接时,您必须更改对链接本身的引用:

but then you will have to change the reference to the link itself when calling it with requests:

a = requests.get(link[1], stream=True)  # (assuming the link is in the second column)

这篇关于重命名使用 Python 请求下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆