I’m trying to download files with this URL: https://mywebsite.com/DATA.zip where DATA is supposed to be numbers from 1 to 2000 (consecutively)
I tried the range() feature but it didn’t work
import requests
session = requests.Session()
url = "https://mywebsite.com/", range(1, 2000),".zip"
response = session.get(url)
file_Path = response.url
if response.status_code == 200:
with open(file_Path, 'wb') as file:
file.write(response.content)
print('File downloaded successfully')
L Worlds Gaming is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
There are a few issues with your code. I expect that you get the error:
response = session.get(url)
InvalidSchema: No connection adapters were found for "('https://mywebsite.com/', range(1, 2000), '.zip')"
This is because the URL you have constructed is a tuple not a string with a valid URL. The range
command doesn’t work as you think it does and neither does string concatenation. range
produces an iterator, which means you have to iterate over its returned values in a loop, so you’d want something like:
# use range from 1 (by default it starts from 0)
for i in range(1, 2001):
session = requests.Session()
# construct the URL using f-string
url = f"https://mywebsite.com/{i}.zip"
response = session.get(url)
# use the number.zip part of the URL for saving
file_Path = response.url.split("/")[-1]
# alternatively (and better!), specify the full path, e.g.
#file_Path = rf"C:UsersMyName{i}.zip"
# which will work provide the directory exists
if response.status_code == 200:
with open(file_Path, 'wb') as file:
file.write(response.content)
print('File downloaded successfully')
4