import requests, json from datetime import datetime import time import sys headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie':'fingerprint=e5e66bd15b0c457ca060f8cb099a69294; loginTime=1690176693133; fingerprint=hyqtq34fqvwxo0mez56g6nmeghu2ip1alsai4ipykxtbohr39ylogud; pgv_pvid=9052429358; backup_cdn_domain=res.wx.qq.com; RK=ibWIwwqIZ3; ptcz=1a80cd4046f3e58dbd941b4a76174d4977265589177c9caef884a16bd9992b5d; low_login_enable=1; tdoc_uid=13102702970838080; wedoc_openid=wozbKqDgAAYt6fSGwDXda25jv2p5OP0w; docMessageCenterCookie=; optimal_cdn_domain=docs2.gtimg.com; wedoc_sid=1kBCdYycYVcuLGgxAMRUNAAA; wedoc_sids=13102701888889220&1oQwT4xhZXMur2NhAINNSgAA|13102702970838080&1kBCdYycYVcuLGgxAMRUNAAA; wedoc_skey=13102701888889220&ac00476fed4403712cae26060dfb39b6||13102702970838080&0c0ef927cbc73439fee11e033cf4bdde; wedoc_ticket=13102701888889220&CAESIG807y5KZMJpoC13g7JnkNY7pdaXE4n_sycBDYT9KEnH|13102702970838080&CAESIAasGW9d_rOy1Vsa38d9xBy8rZdhXF8_Dox8yDl3dYg7; TOK=86b51da63c2ba485; traceid=86b51da63c; hashkey=86b51da6; window_width=701', } sid = '1kBCdYycYVcuLGgxAMRUNAAA' log = '' def request_download(operationId): milliseconds = int(time.time() * 1000) url2 = f"https://doc.weixin.qq.com/v1/export/query_progress?operationId={operationId}×tamp={milliseconds}" second = requests.get(url2, headers=headers) return second.json() def download_file(name, url): global log reqs = requests.get(url, headers=headers) if reqs.status_code == 200: with open(f"./Datas/{name}", "wb") as f: f.write(reqs.content) log = log + f"下载完成:{name}\n" return True else: log = log + f"下载失败:{name}\n" return False def request_operationId(docId): url = f"https://doc.weixin.qq.com/v1/export/export_office?sid={sid}&wedoc_xsrf=1&docId={docId}&version=2" #print(url) first = requests.post(url, headers=headers) return first.json() def download_single_file(name, docId): global log result = True log = log + f"开始下载文件:{name}\n" odata = request_operationId(docId) if odata.get("ret") == 0: operationId = odata.get("operationId") while True: rddata = request_download(operationId) if rddata.get("ret") == 0: if rddata.get("progress") < 100: time.sleep(0.1) else: download_url = rddata.get("file_url") #print(download_url) if not download_file(name, download_url): result = False break else: log = log + f"获取下载链接失败: {name}, rdddata={rddata}\n" result = False break else: result = False log = log + f"获取operationId失败: {name}, odata={odata}\n" file = open("./download.log", "a") file.write(log) file.close() return result def write_file(path, content): file = open(path, "w") file.write(content) file.close()