Python请求modu下载速度低得离谱

2024-09-19 23:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:

我一直在尝试使用Python的请求模块制作一个简单的动画下载器。我使用progressbar2模块跟踪进度。在尝试下载时,我得到的速度是0.x B/s。我假设问题是基于this question选择chunk_size。但不管区块大小,我都能得到同样微不足道的速度。在

规格和信息:

  1. 我使用的是windows10,python3.5,最新的requests模块(2.18.4),有一个不错的网速为40Mbps。在
  2. 我可以通过浏览器(Chrome)从链接下载该文件,并且免费下载 下载管理器大约1分钟。在
  3. 链接是完美的工作,我没有防火墙冲突。在

代码:

import os
import requests
import progressbar
from progressbar import *

os.chdir('D:\\anime\\ongoing')

widgets = ['Downloading: ', Percentage(), ' ', Bar(marker='#',left='[',right=']'),
           ' ', ETA(), FileTransferSpeed()]

url = 'https://lh3.googleusercontent.com/AtkUe87GbrINzTJS_Fj4W08CGqlOg9anwEF7n5-eKXcyS1RsaB8LdzRVaXloiJwiaX2IX1xqUiA=m22?title=(720P%20-%20mp4)Net-juu%20no%20Susume%20Episode%207'
r = requests.get(url,stream=True)
remotesize = r.headers['content-length']

print("Downloading {}.mp4!\n\n".format(url.split('title=')[1]))
pbar = ProgressBar(max_value=int(remotesize),widgets=widgets).start()
i = 0
with open('./tempy/tempy_file.mp4', 'wb') as f:
   for chunk in r.iter_content(chunk_size=5*1024*1024): 
      if chunk:
         i = i + len(chunk)
         f.write(chunk)
         pbar.update(int(i/int(remotesize) * 100))
pbar.finish()         
print("Successfully downloaded!\n\n")

截图:

The speed is just ridiculous.

预期解决方案:

不确定这个Github Issue是否已修复。

  1. 最好是在requests模块中找到一个解决方案,但我愿意接受Python范围内的任何可以让我获得很好速度的答案。在
  2. 我希望下载是分块的,因为我想通过progressbar查看进度。所以shutil.copyfileobj(r.raw)不是我要找的。在
  3. 我确实尝试过使用多个线程,但这只是复杂的事情,并没有帮助。我认为问题在于将块写入缓冲区本身,而在线程之间分割任务并没有帮助。在

编辑:

根据建议,我尝试使用随机用户代理,如下所示:

^{pr2}$

并发送头为r = requests.get(url,stream=True,headers=random_headers())的请求

然而,这没有什么不同。:(

编辑2:

用“http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4”中的示例视频进行了尝试。同样的问题依然存在。:/


Tags: 模块importurlsize链接widgetsrequests速度
2条回答

你有没有试过用你的用户代理和其他Google可能需要的头来填充你的请求头,这样就不会把你标记为机器人,限制你的下载速度?在

所以就像其他人所说的那样,谷歌正在放慢速度。为了克服这个问题,我使用Selenium webdriver下载了以下链接:

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : dir_name}
            chrome_options.add_experimental_option('prefs', prefs)
            driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(li)

好吧,至少我可以用googlechrome的下载程序以尽可能快的速度完全自动化下载。在

因此,如果有人能帮我解决这个问题,请在评论中回复,如果有帮助,我会投赞成票:

  1. 找出一种在Python中为每个文件使用多个连接的方法,就像Free Download Manager使用的方式一样。在

以下是指向完整的script的链接。在

相关问题 更多 >