How to download chromedriver instead of webpage with python requests library

Question:

Today I’m creating a Python script to find a specific version of chromedriver here.
My intention is to make a request to the URL containing the specific version of chromedriver I need to download and save it to the same directory as the script. However, each time I run my code to install a file, say chromedriver_win32.zip in this folder, I end up downloading the webpage it is stored on instead of the file itself.

Here’s my script:

#Get the exact folder name containing the version we need to download
chromedriverVersionUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_100"
response = requests.get(chromedriverVersionUrl)
latestVersionNumber = response.text

#Target the folder of the exact version we need to download and download it!
downloadUrl = "https://chromedriver.storage.googleapis.com/index.html?path=" + latestVersionNumber + "/chromedriver_win32.zip"

#Make the request and write the response to a file
r = requests.get(downloadUrl, allow_redirects=True)
open('chromedriver.zip', 'wb').write(r.content)

Every time, chromedriver.zip ends up being a very small file that windows says is a corrupted zip file. I tried downloading the contents as a .txt file and it turned out I was just downloading the webpage.

I have tried using wget and the dload libraries to download this file in addition to requests, but they have all yielded the same result. Could someone please show me what I might be doing wrong?

Asked By: chefjeff

||

Answers:

The paths you are using do not correspond with that is found in the folder:

You are downloading this:

downloadUrl = "https://chromedriver.storage.googleapis.com/index.html?path=" + latestVersionNumber + "/chromedriver_win32.zip"

But if you check the link when you access the example website:

https://chromedriver.storage.googleapis.com/index.html?path=100.0.4896.60/

It is:

https://chromedriver.storage.googleapis.com/100.0.4896.60/chromedriver_win32.zip

Sou you will have to build your link as:

version = "100.0.4896.60"
url = f"https://chromedriver.storage.googleapis.com/{version}/chromedriver_win32.zip"

Instead of using the query parameter that you where using before:

version = "100.0.4896.60"
url = f"https://chromedriver.storage.googleapis.com/index.html?path={version}/"
Answered By: nck