繁体   English   中英

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

[英]How to translate url encoded string 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”

您可以使用urllib2 unquote函数:

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

用这个:

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

您可以在这里找到更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM