爬蟲提取的數據以excel的形式發送到釘釘


第一步:獲取access_token 文檔:釘釘開發文檔

第二步:上傳文件,獲取media_id 文檔:釘釘開發文檔

第三步:使用釘釘機器人發送下載鏈接 文檔:釘釘開發文檔

第四步:代碼編寫

import requests import json from urllib3 import encode_multipart_formdata import time def getToken(): ''' 獲取最新的access_token''' corpid = 'xxxxxx' secrect = 'xxxxxxxxxx' url = 'https://oapi.dingtalk.com/gettoken?corpid=%s&corpsecret=%s' % (corpid, secrect) req = requests.get(url) access_token = json.loads(req.text) return access_token['access_token'] def get_media_id(file_path,file_name,access_token): '''上傳文件並且返回對應的media_id''' url_post=r"https://oapi.dingtalk.com/media/upload?access_token=%s&type=file"%access_token headers={} data={} data['media']= (file_name, open(file_path, 'rb').read()) #說明:file_name,不支持中文,必須為應為字符 encode_data = encode_multipart_formdata(data) data = encode_data[0] headers['Content-Type'] = encode_data[1] r = requests.post(url_post, headers=headers, data=data) media_id=json.loads(r.text)["media_id"] return media_id def send_file(access_token,media_id,url_robot,msg,key): '''通過群機器人發送鏈接,達到點擊鏈接下載文件的目的''' header = { "Content-Type": "application/json", "Charset": "UTF-8" } send_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) down_load_url="https://oapi.dingtalk.com/media/downloadFile?access_token=%s&media_id=%s"%(access_token,media_id) data={ "actionCard": { "title": "%s"%key, "text": " %s \n\n更新時間:%s "%(msg,send_time), "hideAvatar": "0", "btnOrientation": "0", "btns": [ { "title": "點擊下載數據", "actionURL": down_load_url }, ] }, "msgtype": "actionCard" } r = requests.post(url_robot,data=json.dumps(data),headers=header) print (r.text) return json.loads(r.text) def send_file_robot(file_path,url_robot,msg,key): '''依次為文件路徑,Webhook地址,需要發送的消息,釘釘安全設置的自定義關鍵字''' access_token=getToken() file_name=file_path.split('\\')[-1] media_id=get_media_id(file_path,file_name,access_token) result=send_file(access_token,media_id,url_robot,msg,key) print(result) if __name__ == "__main__": url_robot='Webhook地址' file_path=r'C:\Users\Administrator\Desktop\file\test.xlsx'#文件路徑 msg='需要說明的話' key='joyooooo' #釘釘安全設置的自定義關鍵字 send_file_robot(file_path,url_robot,msg,key) 

測試效果:

點擊下載數據,彈出下載框:

最終,配合數據提取、拆分模塊和定時任務,便可實現數據文件的定時批量發送了~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM