user9440272
user9440272

Reputation:

How to translate url encoded string python

This code is supposed to download a list of pdfs into a directory

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()

I used os.path.basename() just to make sure the files would actually download. However, I want to know how to change the file name from 3D%20Printing%20Blueprints%20%5BeBook%5D.pdf to something like "3D Printing Blueprints.pdf"

Upvotes: 0

Views: 79

Answers (2)

Hasher
Hasher

Reputation: 62

use this:

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

you can find more info here

Upvotes: 0

TheoretiCAL
TheoretiCAL

Reputation: 20581

You can use the urllib2 unquote function:

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

Upvotes: 1

Related Questions