Downloading video with python requests content receiving status.

huangapple go评论83阅读模式
英文:

downloading video with python requests content receiving status

问题

我正在使用Python的requests模块下载视频。
我想知道当前下载了多少内容,就像在浏览器中看到的那样,已下载x字节,共y字节。

我已经附上了下面的代码,这是我正在使用的。
我该如何实现我上面说的?

print(f'[+] 正在下载 "{file}" ({sizeInMb} MB)...')
with requests.get(url, stream=True) as r:
    with open(filePath, 'wb+') as f:
        shutil.copyfileobj(r.raw, f)
        print(f'[-] 已下载 "{file}".')
        downloadCount += 1
英文:

I am downloading video using python requests module.
I want to know how much content is received currently while downloading like we see in browsers etc. x bytes downloaded of y bytes.

I have attached the code below what I'm using.
How can I achieve what I said above?

print(f'[+] Downloading "{file}" ({sizeInMb} MB)...')
with requests.get(url, stream=True) as r:
    with open(filePath, 'wb+') as f:
        shutil.copyfileobj(r.raw, f)
        print(f'[-] Downloaded "{file}".')
        downloadCount += 1

答案1

得分: 2

要创建一个下载进度条,你可以在Python中使用wget模块。

import wget

url = 'https://www.tutorialspoint.com/python3/python3_tutorial.pdf'
file_name = wget.download(url)

print()
print(file_name)

我在这里使用了一个随机的PDF文件作为参考。你也可以在wget.download函数中使用out参数来更新下载的文件路径和文件名。

最终输出:

100% [..........................................................................] 1015876 / 1015876
python3_tutorial.pdf
英文:

To have a download bar, you can use the wget module in Python.

import wget

url = 'https://www.tutorialspoint.com/python3/python3_tutorial.pdf'
file_name = wget.download(url)

print()
print(file_name)

I used a random PDF as a reference here. You can also update the downloaded file path and filename in the wget.download function, using the out argument.

Final Output:

100% [..........................................................................] 1015876 / 1015876
python3_tutorial.pdf

答案2

得分: 1

你可以使用requests库的iter_content()方法以块的方式下载视频并跟踪下载进度。iter_content()方法返回一个生成器,按照它们被下载的顺序产生文件的块。你可以使用len()函数来获取文件的总大小,然后使用计数器来跟踪到目前为止已下载多少字节。

以下是如何执行这个操作的示例代码:

import requests

def download_video(url):
    response = requests.get(url, stream=True)
    file_size = int(response.headers['Content-Length'])
    downloaded = 0
    with open('video.mp4', 'wb') as f:
        for chunk in response.iter_content(chunk_size=1024):
            downloaded += len(chunk)
            print(f'Downloaded {downloaded}/{file_size} bytes')
            f.write(chunk)

if __name__ == '__main__':
    download_video('https://www.example.com/video.mp4')

输出将会是:

Downloaded 0/1024000 bytes
Downloaded 1024000/1024000 bytes
英文:

you can use the iter_content() method of the requests library to download the video as chunks and track the progress of the download. The iter_content() method returns a generator that yields the chunks of the file as they are downloaded. You can use the len() function to get the total size of the file, and then use a counter to track how many bytes have been downloaded so far.

Here is an example of how to do this:

import requests

def download_video(url):
    response = requests.get(url, stream=True)
    file_size = int(response.headers['Content-Length'])
    downloaded = 0
    with open('video.mp4', 'wb') as f:
        for chunk in response.iter_content(chunk_size=1024):
            downloaded += len(chunk)
            print(f'Downloaded {downloaded}/{file_size} bytes')
            f.write(chunk)

if __name__ == '__main__':
    download_video('https://www.example.com/video.mp4')

output be like:

Downloaded 0/1024000 bytes
Downloaded 1024000/1024000 bytes

huangapple
  • 本文由 发表于 2023年7月6日 18:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定