Renaming the file downloaded with Python Requests

后端 未结 1 796
眼角桃花
眼角桃花 2021-01-26 05:37

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

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

相关标签:
1条回答
  • 2021-01-26 06:39

    Instead of

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

    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)
    
    0 讨论(0)
提交回复
热议问题