Python 3.x Download a file from the internet in python?

Python 3.x Download a file from the internet in python?,python-3.x,python-requests,filepath,Python 3.x,Python Requests,Filepath,Any inputs?

Any inputs?


#1

You are opening a requests object not a file path

file_url = requests.get(downloadUrl)
with open(file_url, 'w+') as f:

You would need to open a filepath on disk somewhere and write to that

See Using the request Module on https://stackabuse.com/download-files-with-python/

EDIT: From comments on post

In [584]: link = "https://www.annualreports.com/HostedData/AnnualReportArchive/3/NASDAQ_QFIN_2018.pdf"

In [585]: import requests

In [586]: file_url = requests.get(link)

In [587]: with  open('/tmp/NASDAQ_QFIN_2018.pdf', 'wb') as f:
    ...:     f.write(file_url.content)
    ...:

In [588]: ls -al /tmp/NASDAQ_QFIN_2018.pdf
-rw-r--r--  1 ME  wheel  1796199  9 Apr 15:21 /tmp/NASDAQ_QFIN_2018.pdf

#2

This looks quite similar to this other recent question.

#3

@GinoMempin yeah! but it is not answered, so I still need an answer sir, Thank you

#4

i think you mean with open(filepath, 'w+') as f: as file_url contains response and not the url of the file you want to create.

#5

Thank you for clarification, but I'm getting a PermissionError: [Errno 13] Error after adding with open(filepath, 'w+') as f:

#6

That is because you are trying to open a Directory not a file you would need to download to C:\Users\PacY\Downloads\myfilename.whatever

#7

Okay. I have done it. Now I can enter a path as userinput refer to edited codes above. the script will download the file in the specified path, but the problem the downloaded file will be empty. 0KB

#8

What is an example URL you are trying to download?