谷歌云盘批量下载文件
生活随笔
收集整理的這篇文章主要介紹了
谷歌云盘批量下载文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景:公司的網絡上不了driver.google.com,卻能夠上得了googleusercontent.com (這是谷歌文件下載的直鏈),然后文件一大,下載到一半老是斷,所以要避免文件夾直接壓縮下載,只能一個一個下載,
一:首先第一步,要把要下載的文件夾分享出去,直接在文件夾那里右鍵-》共享-》記得選擇知道鏈接的任何人,然后你的文件上就有一對情侶
注意:如果文件夾里的文件特別多,要滾動鼠標,然后所有文件都在列表了
二:按F12打開瀏覽器控制臺
輸入:
for(i=0;i<document.getElementsByClassName("WYuW0e").length;i++){console.log(document.getElementsByClassName("WYuW0e")[i].getAttribute("data-id"))}得到
這些就是文件的ID,之后拿著這個ID,去網上找大佬無私奉獻的直鏈轉換工具,就能夠得到直鏈下載地址,我這里用的是:
https://api.moeclub.org/GoogleDrive/這個鏈接后面拼上id就能夠獲取到直鏈了
然后上代碼:
# ids獲取方式 # 在谷歌云盤瀏覽器控制臺輸入 # for(i=0;i<document.getElementsByClassName("WYuW0e").length;i++){console.log(document.getElementsByClassName("WYuW0e")[i].getAttribute("data-id"))} from contextlib import closingids = ["18jWt4ak1zK1PHTFAVPojFJh9n_sYz","balabalablalalalfdsaldfas", ] uri = "https://api.moeclub.org/GoogleDrive/" outpath = "E:\\" allok = [] allerr = [] import requests# proxies = { # 'http': 'http://127.0.0.1:1080', # 'https': 'http://127.0.0.1:1080' # https -> http # }okfile = r'ok.txt' errfile = r'err.txt'for id_ in ids:try:print(f"開始請求下載{id_}")with closing(requests.get(uri + id_, allow_redirects=True, stream=True)) as myfile:# myfile = requests.get(uri + id_, allow_redirects=True, stream=True)print(f"獲取下載鏈接成功,開始獲取文件名")print(f"鏈接為:{myfile.url}")filename = myfile.headers.get("content-disposition").split("filename=")[1].replace("\"", "").split(";")[0]print(f"文件名為{filename}")filepath = outpath + filenameprint(f"開始保存到路徑{filepath}")fh = open(filepath, 'wb')print(f"開始下載{filename}")filesize = int(myfile.headers.get('content-length'))print(f"文件大小為:{filesize/1024/1024} MB")downloaded = 0perper = 0for chunk in myfile.iter_content(chunk_size=2048): # 每次下載5120,因為我的大點,我選擇每次稍大一點,這個自己根據需要選擇。downloaded += 2048per = downloaded/filesize*100if per - perper >= 1:print(f"下載進度為:{per} %")perper = perif chunk:fh.write(chunk)# print(f"獲取下載鏈接成功,開始獲取文件名")# print(f"鏈接為:{myfile.url}")# filename = myfile.headers.get("content-disposition").split("filename=")[1].replace("\"", "").split(";")[0]# print(f"文件名為{filename}")# filepath = outpath + filename# print(f"開始保存到路徑{filepath}")# fh = open(filepath, 'wb')# fh.write(myfile.content)print(f"文件 {filename} 下載成功,并保存到了 {filepath}")allok.append(id_ + "," + filename)with open(okfile, 'a+') as ok:ok.write(id_ + "," + filename + "\n") # 加\n換行顯示except BaseException as e:allerr.append(id_)with open(errfile, 'a+') as err:err.write(id_ + "\n") # 加\n換行顯示print(f"發(fā)生異常: {e}")continueelse:fh.close() print(allok) print(allerr) exit()總結
以上是生活随笔為你收集整理的谷歌云盘批量下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软应提前发布专用于ARM处理器的Win
- 下一篇: 怎么查看本机的IP地址