python爬虫系列之下载在线文档Excel(腾讯)

一、简介

本文讲述使用python下载腾讯文档中的Excel数据。

  1. 思路
腾讯文档导出流程如下
使用抓包工具获取导出接口,检测数据是否准备完成接口、下载excel接口,使用requests进行调用,即可完成下载

二、实现步骤

1. 数据准备

    提供腾讯文档中excel文档地址,以便进行下载,首先需要进行登陆,获取excel文档地址 获取文档localPadId,每份文档唯一。打开浏览器控制台,刷新页面,filter中输入doc_info,找到该请求的localPadId local_pad_id = 300000000$TfQWCVvRALvN cookie值获取,右键复制即可 cookie_value = ********

2. 获取当前用户nowUserIndex

用户nowUserIndex字段为后续下载excel文件接口的u字段,这里通过调用接口document_url获取

def get_now_user_index(self):
        """
        # 获取当前用户信息,供创建下载任务使用
        :return:
            # nowUserIndex = 4883730fe8b94fbdb94da26a9a63b688
            # uid = 144115225804776585
            # utype = wx
        """
        response_body = requests.get(url=self.document_url, headers=self.headers, verify=False)
        parser = BeautifulSoup(response_body.content, html.parser)
        global_multi_user_list = re.findall(re.compile(window.global_multi_user=(.*?);), str(parser))
        if global_multi_user_list:
            user_dict = json.loads(global_multi_user_list[0])
            print(user_dict)
            return user_dict[nowUserIndex]
        return cookie过期,请重新输入

3.创建导出任务

def export_excel_task(self, export_excel_url):
        """
        导出excel文件任务,供查询文件数据准备进度
        :return:
        """
        body = {
          
   
            docId: self.localPadId, version: 2
        }

        res = requests.post(url=export_excel_url,
                                      headers=self.headers, data=body, verify=False)
        operation_id = res.json()[operationId]
        return operation_id

4. 检查数据准备进度,并下载

def download_excel(self, check_progress_url, file_name):
        """
        下载excel文件
        :return:
        """
        # 拿到下载excel文件的url
        start_time = time.time()
        file_url = 
        while True:
            res = requests.get(url=check_progress_url, headers=self.headers, verify=False)
            progress = res.json()[progress]
            if progress == 100:
                file_url = res.json()[file_url]
                break
            elif time.time() - start_time > 30:
                print("数据准备超时,请排查")
                break
        if file_url:
            self.headers[content-type] = application/octet-stream
            res = requests.get(url=file_url, headers=self.headers, verify=False)
            with open(file_name, wb) as f:
                f.write(res.content)
            print(下载成功,文件名:  + file_name)
        else:
            print("下载文件地址获取失败, 下载excel文件不成功")

三、完整代码

四、效果演示

成功下载的文档如下所示

经验分享 程序员 微信小程序 职场和发展