Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重命名使用Python请求下载的文件_Python_Python 3.x_Web Scraping_Python Requests - Fatal编程技术网

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

重命名使用Python请求下载的文件,python,python-3.x,web-scraping,python-requests,Python,Python 3.x,Web Scraping,Python Requests,如何替换随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/Present

如何替换随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
我当前的代码:

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文件中的特定列:

pdf_file = link[1]  # (assuming the file name is in the second column)
如果文件名在第一列中,则应使用

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)
而不是

pdf_file = link[0].split('/')[-1]
使用csv文件中的特定列:

pdf_file = link[1]  # (assuming the file name is in the second column)
如果文件名在第一列中,则应使用

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)

它起作用了。但是,它的保存没有任何
文件类型
,如果我在第1列中有2或3个相同的名称,它会一次又一次地替换该文件+我如何在文件名中添加
时间戳
,这样它就不会用相同的名称替换该文件@errata@WarLock当然,它将用相同的名称替换文件:)您必须确保所有名称都是唯一的。这是每个操作系统的“功能”。。。我更新了我的答案,为每个文件名添加了时间戳。如果在相同的
manual\u name
前面的B、C、D列中也有多个链接,请使用save name保存。我们怎么也能读到这个链接呢@errata@WarLock文件中的每一行都是一个元素,您可以像访问任何其他列表一样访问元素。此外,请避免对问题/答案进行大量评论,因为这不是目的()。提出一个新问题或在提问前进行适当的研究;)对不起下次我会记住的。:)@勘误表起作用了。但是,它的保存没有任何
文件类型
,如果我在第1列中有2或3个相同的名称,它会一次又一次地替换该文件+我如何在文件名中添加
时间戳
,这样它就不会用相同的名称替换该文件@errata@WarLock当然,它将用相同的名称替换文件:)您必须确保所有名称都是唯一的。这是每个操作系统的“功能”。。。我更新了我的答案,为每个文件名添加了时间戳。如果在相同的
manual\u name
前面的B、C、D列中也有多个链接,请使用save name保存。我们怎么也能读到这个链接呢@errata@WarLock文件中的每一行都是一个元素,您可以像访问任何其他列表一样访问元素。此外,请避免对问题/答案进行大量评论,因为这不是目的()。提出一个新问题或在提问前进行适当的研究;)对不起下次我会记住的。:)@勘误表