python request下载文件_Python3.4.3使用urllib.request下载文件带进度显示
import?urllib.request
import?sys
import?os
import?threading
def?callbackfunc(blocknum,?blocksize,?totalsize):
'''回調函數
@blocknum:?已經下載的數據塊
@blocksize:?數據塊的大小
@totalsize:?遠程文件的大小
'''
global?url
percent?=?100.0?*?blocknum?*?blocksize?/?totalsize
if?percent?>?100:
percent?=?100
downsize=blocknum?*?blocksize
if?downsize?>=?totalsize:
downsize=totalsize
s?="%.2f%%"%(percent)+"====>"+"%.2f"%(downsize/1024/1024)+"M/"+"%.2f"%(totalsize/1024/1024)+"M?\r"
sys.stdout.write(s)
sys.stdout.flush()
if?percent?==?100:
print('')
input('輸入任意鍵繼續...')
def?downimg():
url='http://dlsw.baidu.com/sw-search-sp/soft/e7/10520/KanKan_V2.7.8.2126_setup.1416995191.exe'
filename=os.path.basename(url)
urllib.request.urlretrieve(url,?filename,?callbackfunc)
啟動線程下載
threading.Thread(target=downimg,args=('')).start()
總結
以上是生活随笔為你收集整理的python request下载文件_Python3.4.3使用urllib.request下载文件带进度显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python pandas教程百家号_p
- 下一篇: 苹果自动驾驶汽车两个月出了16次车祸 远