Python:从可点击的链接下载文件,点击后开始下载 [英] Python: download a file from a clickable link that when clicked starts downloading

查看:66
本文介绍了Python:从可点击的链接下载文件,点击后开始下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 URL 链接,如果我点击它,它将开始下载文件.网址如下所示:

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

如果我复制 URL 并粘贴它,它也会在网络浏览器的 URL 栏中开始下载.

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

如何使用 Python 下载文件,最好不使用 selenium.

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

推荐答案

您可以使用 requests 模块下载文件:

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)

注意事项:

  1. 用你想要的 URL 替换 http://somewebsite.com/download?f=someStrings

用文件名及其文件扩展名替换filename.extension.

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

这篇关于Python:从可点击的链接下载文件,点击后开始下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆