从 URL 下载文件或视频 (Python 3)

发布于 2025-01-20 06:34:14 字数 207 浏览 1 评论 0原文

我尝试了不同的libs以从URL下载视频。但是,即使他们中的一个也没有起作用。 这是ı尝试的链接:https://td-cdn.pw/api.php?download=tikdown.org-42500282235.mp4

如果打开一次,它直接要求下载,不喜欢HTML视频。我想将此视频保存到本地文件夹。

如果你们帮助我很自豪:)(顺便说一句

ı tried diffrent libs to download video from url. But even one of them didnt worked.
Here is the link, that ı trying: https://td-cdn.pw/api.php?download=tikdown.org-42500282235.mp4

If it opened once, it directly asking to download, not like a html video. And ı want to save this video to local folder.

If you guys help me ı would be so proud :) (btw ım freaking ı try to solve it last 4 hours)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2025-01-27 06:34:14

在Python中下载文件有两个步骤,因此该过程是独立的。我建议使用内置请求库。我们使用它来向服务器提出请求并获取内容。然后,我们将数据写入下一步。

import requests

URL = "https://td-cdn.pw/api.php?download=tikdown.org-42500282235.mp4"
FILE_TO_SAVE_AS = "myvideo.mp4" # the name you want to save file as


resp = requests.get(URL) # making requests to server

with open(FILE_TO_SAVE_AS, "wb") as f: # opening a file handler to create new file 
    f.write(resp.content) # writing content to file

但这只是一个简单的例子。您可以实现其他功能,例如try/catch块,以捕获任何异常或在提出请求时使用自定义标头。

There are two steps to getting file downloaded in Python so the process is os independant. I would recommend using inbuilt requests library. We use it to make requests to server and fetch content. Then we write the data into a file in next step.

import requests

URL = "https://td-cdn.pw/api.php?download=tikdown.org-42500282235.mp4"
FILE_TO_SAVE_AS = "myvideo.mp4" # the name you want to save file as


resp = requests.get(URL) # making requests to server

with open(FILE_TO_SAVE_AS, "wb") as f: # opening a file handler to create new file 
    f.write(resp.content) # writing content to file

But this is just a simple example. You can implement other features like try/catch blocks to catch any exceptions or use custom headers while making requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文