首页 > 解决方案 > 如何翻译url编码的字符串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”更改为

标签: python

解决方案


您可以使用urllib2 取消引用功能:

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

推荐阅读