爬蟲提取的資料以excel的形式傳送到釘釘
阿新 • • 發佈:2020-08-17
第一步:獲取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": [