如何翻译url编码的字符串python

时间:2018-04-24 22:06:33

标签: python

此代码应该将pdf列表下载到目录

for pdf in preTag:
    pdfUrl = "https://the-eye.eu/public/Books/Programming/" + 
    pdf.get("href")
    print("Downloading...%s"% pdfUrl)
    #downloading pdf from url
    page = requests.get(pdfUrl)
    page.raise_for_status()

    #saving pdf to new directory
    pdfFile = open(os.path.join(filePath, os.path.basename(pdfUrl)), "wb")
    for chunk in page.iter_content(1000000):
        pdfFile.write(chunk)
pdfFile.close()

我使用os.path.basename()只是为了确保文件实际下载。但是,我想知道如何将文件名从3D%20Printing%20Blueprints%20%5BeBook%5D.pdf更改为“3D Printing Blueprints.pdf”

2 个答案:

答案 0 :(得分:1)

您可以使用urllib2 unquote功能:

import urllib2
print urllib2.unquote("3D%20Printing%20Blueprints%20%5BeBook%5D.pdf") #3D Printing Blueprints.pdf

答案 1 :(得分:0)

使用它:

os.rename("3D%20Printing%20Blueprints%20%5BeBook%5D.pdf", "3D Printing Blueprints.pdf")

您可以找到更多信息here