重命名使用Python请求下载的文件

2024-09-30 01:37:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我想将其另存为Manual_name1.pdf,而不是Elkinson%20Jeffrey.pdf

CSV文件看起来像:

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

我的当前代码:

^{pr2}$

Tags: 文件ofcsvhttpsorg名称httpdoc
1条回答
网友
1楼 · 发布于 2024-09-30 01:37:41

而不是

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

使用csv文件中的特定列:

^{pr2}$

如果文件名在第一列中,则应使用

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"

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

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

相关问题 更多 >

    热门问题