python爬虫系列之下载在线文档Excel(腾讯)
python爬虫系列之腾讯文档Excel数据
- 一、简介
- 二、实现步骤
- 1. 数据准备
- 2. 获取当前用户nowUserIndex
- 3.创建导出任务
- 4. 检查数据准备进度,并下载
- 三、完整代码
- 四、效果演示
一、简介
本文讲述使用python下载腾讯文档中的Excel数据。
- 思路
腾讯文档导出流程如下
使用抓包工具获取导出接口,检测数据是否准备完成接口、下载excel接口,使用requests进行调用,即可完成下载
二、实现步骤
1. 数据准备
- 提供腾讯文档中excel文档地址,以便进行下载,首先需要进行登陆,获取excel文档地址
document_url = https://docs.qq.com/sheet/DSnhHWFRraGRzSXhC
- 获取文档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.创建导出任务
创建导出任务的接口通过点击导出按钮获取,其中u字段值即为nowUserIndex字段,创建导出任务接口会返回一个operationId字段,是检测数据准备进度的入参。
export_excel_url = f'https://docs.qq.com/v1/export/export_office?u={now_user_index}'
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. 检查数据准备进度,并下载
查询数据准备进度的接口为 check_progress_url = f'https://docs.qq.com/v1/export/query_progress?u={now_user_index}&operationId={operation_id}'
,在点击导出excel时会出现该接口信息,频繁调用该接口,当progress进度为100时,该接口返回值会出现file_url信息。然后requests调用file_url,就可以拿到文件流,写到excel文件中即可完成下载。
def download_excel(self, check_progress_url, file_name):"""下载excel文件:return:"""# 拿到下载excel文件的urlstart_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']breakelif time.time() - start_time > 30:print("数据准备超时,请排查")breakif 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文件不成功")
三、完整代码
# -*- coding: UTF-8 -*-
"""
@Project :small-tools
@File :tengxun.py
@Author :silen
@Time :2022/5/26 15:42
@Description :
"""
import json
import os
import re
import time
from datetime import datetime
from time import sleep
import click
import pandas as pd
import requests
from bs4 import BeautifulSoupclass TengXunDocument():def __init__(self, document_url, local_pad_id, cookie_value):# excel文档地址self.document_url = document_url# 此值每一份腾讯文档有一个,需要手动获取self.localPadId = local_pad_idself.headers = {'content-type': 'application/x-www-form-urlencoded','Cookie': cookie_value}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过期,请重新输入'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_iddef download_excel(self, check_progress_url, file_name):"""下载excel文件:return:"""# 拿到下载excel文件的urlstart_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']breakelif time.time() - start_time > 30:print("数据准备超时,请排查")breakif 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文件不成功")if __name__ == '__main__':# excel文档地址document_url = 'https://docs.qq.com/sheet/DSnhHWFRraGRzSXhC'# 此值每一份腾讯文档有一个,需要手动获取local_pad_id = '300000000$JxGXTkhdsIxB'# 打开腾讯文档后,从抓到的接口中获取cookie信息cookie_value = '****'tx = TengXunDocument(document_url, local_pad_id, cookie_value)now_user_index = tx.get_now_user_index()# 导出文件任务urlexport_excel_url = f'https://docs.qq.com/v1/export/export_office?u={now_user_index}'# 获取导出任务的操作idoperation_id = tx.export_excel_task(export_excel_url)check_progress_url = f'https://docs.qq.com/v1/export/query_progress?u={now_user_index}&operationId={operation_id}'current_datetime = datetime.strftime(datetime.now(), '%Y_%m_%d_%H_%M_%S')file_name = f'{current_datetime}.xlsx'tx.download_excel(check_progress_url, file_name)
四、效果演示
成功下载的文档如下所示
如果我的文章对你有帮助,感谢你点的关注~
python爬虫系列之下载在线文档Excel(腾讯)相关推荐
- Python爬虫 —— 使用BeautifulSoup4解析HTML文档
Python爬虫 -- 使用BeautifulSoup4解析HTML文档 目录 Python爬虫 -- 使用BeautifulSoup4解析HTML文档 1.BeautifulSoup4简介 1.1 ...
- Excel函数实现腾讯在线文档Excel会员功能——标记重复值
问题描述 当前在腾讯在线文档,我们需要对重复内容进行判断,很显然作为程序员,不能人工去一个个看,想通过Excel中内置的一些函数或者一些条件高亮实现. 于是,在腾讯文档中寻找,发现了一个符合我们要求的 ...
- python最新版本安卓下载_Python文档app最新版下载-Python文档app安卓版下载v1.0- 游侠下载站...
Python文档是一款很棒的手机编程文档阅读app,用户可以在这里进行快速的Python文档的阅读和修改,非常的简单方便,这款软件很适合那些学习编程的朋友使用,这里还能对语言的性质进行解释,需要的朋友 ...
- 如何实现在线文档?Vue+SpreadJS组合拳出击
SpreadJS v13.2正式版下载 在线文档,顾名思义就是通过在线的方式对文档进行操作,实现如数据填报.数据计算.可视化.在线导入导出 Excel 文件.自定义系统外观.工具栏.在网页内满足单人或 ...
- 自己动手将在线文档制作成CHM文件
在线帮助文档转CHM方法 用到的工具: wget工具(Linux自带,Windows下请自行下载),htm2chm(共享软件,不过没什么限制) 具体步骤: 1.使用wget工具下载在线文档整个目录 简 ...
- 鸿蒙文档旗舰店,腾讯文档鸿蒙版下载-腾讯文档 鸿蒙版v2.11.0-PC6鸿蒙网
腾讯文档app是一款在线文档编辑应用.腾讯文档app支持多人协作编辑Word.Excel.PPT等内容,可以设置权限,文档安全可控. 软件介绍 腾讯文档app是一款可多人协作的在线文档.腾讯文档app ...
- vue+ts在线文档编辑(类腾讯文档)多人在线编辑-自定义页眉和分页打印(三)
目录 前言 一.自定义页眉内容 二.分页文档打印 1.加入分页符 2.打印方法 3.打印模块完整代码 总结 前言 随着在线办公场景越来越多,同时需要各式各样办公软件,在开发时就用得到在线文档来内容指定 ...
- 腾讯在线文档发布:实现QQ、微信多平台多人协作编辑
18日,腾讯宣布推出专注多人协作的在线文档产品-腾讯文档,据介绍,腾讯文档是一款支持随时随地创建.编辑的多人协作式在线文档工具,拥有一键翻译.实时股票函数和浏览权限安全可控等功能,以及打通QQ.微信等 ...
- 在线文档技术-编辑器篇
这是在线文档技术的第二篇文章,本文将对目前市面上所有的主流编辑器和在线文档进行一次深入的剖析和研究,从而使大家对在线文档技术有更深入的了解,也让更多人能够参与其开发与设计中来. 注意:出于对主流文档产 ...
最新文章
- 点站点链接出现短时间白屏或闪屏现象
- Silverlight 解谜游戏 之十四 音效
- Maven : Log4j2 could not find a logging implementation
- MySQL学习之备份
- es获取最大时间的记录_Python日志写入ES之五种方案比较
- html5动画测试题,Html5+js测试题
- RFID电力设备智能巡检管理解决方案
- SQL注入 时间延时注入语句
- jquery html 兼容ie8,IE浏览器 对 jquery版本的兼容性 支持
- 重庆python爬虫培训_PYTHON爬虫工程师
- oracle 查历史数据,Oracle 查询历史数据(转帖)
- cpu虚拟化(cpu虚拟化开启有什么影响)
- 关于airdroid免root操作
- 易基因 | 宏基因组测序带给了我们什么?
- TCP/IP协议就是这个玩意(不看也不影响拿到50K!但是程序员应该知道这些)持续更新中
- 通达OA 2011-2013 通杀
- 语音信号线性预测分析
- 应用宝SDK YSDK登录验证服务端
- Python环境搭建以及简单入门介绍
- DeepLabV3网络简析