EASY PYTHON SELENIUM:如何在不使用 urllib 的情况下下载 mp4?

发布于 2025-01-10 14:04:27 字数 537 浏览 0 评论 0原文

我正在尝试下载此视频: https ://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4

我尝试了以下方法,但它不起作用。

link = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"
urllib.request.urlretrieve(link, 'video.mp4')

我得到:

urllib.error.HTTPError: HTTP Error 403: Forbidden

是否有另一种方法可以在不使用 urllib 的情况下下载 mp4 文件?

I'm trying to download this video: https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4

I tried the following but it doesn't work.

link = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4"
urllib.request.urlretrieve(link, 'video.mp4')

I'm getting:

urllib.error.HTTPError: HTTP Error 403: Forbidden

Is there another way to download an mp4 file without using urllib?

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

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

发布评论

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

评论(1

会傲 2025-01-17 14:04:27

我使用模块 requests 下载没有问题,

import requests

url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4'

response = requests.get(url)

with open('video.mp4', 'wb') as f:  # use `"b"` to open in `bytes mode`
    f.write(response.content)       # use `.content` to get `bytes`

它是小文件 ~10MB,但对于更大的文件,您可以分块下载。

import requests

url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4'

response = requests.get(url, stream=True)

with open('video.mp4', 'wb') as f:
    for chunk in response.iter_content(10000):  # 10_000 bytes
        if chunk:
            #print('.', end='')  # every dot will mean 10_000 bytes 
            f.write(chunk)

文档显示流请求,但针对的是文本数据。


url 是一个 string,因此您可以使用 string 函数来获取最后一个 / 之后的元素

filename =  url.split('/')[-1]

,或者您可以尝试use os.path

至少它可以在 Linux 上工作 - 也许是因为 Linux 也在本地路径中使用 /

import os

head, tail = os.path.split(url)

# head: 'https://www.learningcontainer.com/wp-content/uploads/2020/05'
# tail: 'sample-mp4-file.mp4'

I have no problem to download with module requests

import requests

url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4'

response = requests.get(url)

with open('video.mp4', 'wb') as f:  # use `"b"` to open in `bytes mode`
    f.write(response.content)       # use `.content` to get `bytes`

It was small file ~10MB but for bigger file you may download in chunks.

import requests

url = 'https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4'

response = requests.get(url, stream=True)

with open('video.mp4', 'wb') as f:
    for chunk in response.iter_content(10000):  # 10_000 bytes
        if chunk:
            #print('.', end='')  # every dot will mean 10_000 bytes 
            f.write(chunk)

Documentation shows Streaming Requests but for text data.


url is a string so you can use string-functions to get element after last /

filename =  url.split('/')[-1]

Or you can try to use os.path

At least it works on Linux - maybe because Linux also use / in local paths.

import os

head, tail = os.path.split(url)

# head: 'https://www.learningcontainer.com/wp-content/uploads/2020/05'
# tail: 'sample-mp4-file.mp4'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文