简体   繁体   中英

Python: download a file from a clickable link that when clicked starts downloading

I have a URL link that if I click on it, it will start downloading a file. The URL looks something like this:

http://somewebsite.com/download?f=someStrings

If I copy the URL and past it, in the URL bar of a web-browser it will start downloading as well.

How can I download the file using Python and preferably without using selenium.

You can download the file using the requests module:

import requests as rq
r = rq.get('http://somewebsite.com/download?f=someStrings', allow_redirects=True)
open('filename.extension', 'wb').write(r.content)

Some points to note:

  1. Replace http://somewebsite.com/download?f=someStrings with your desired URL

  2. Replace filename.extension with file name and its file extension.

  3. You can also specify the path where you want to save it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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