脚本定时发送微博热搜关键词到微信企业号
· 技术积累 · Python Linux Shell

shell脚本获取,python脚本发送。此篇文章需要有企业微信管理权限才能使用

1、企业微信(企业号)现在申请要企业验证,我申请的比较早当时不需要验证
2、crontab计划任务
3、python
4、个人微信加入企业中,个人微信关注企业号,然后把人员加入到部门,消息按部门发送

效果展示(因为微信API接口文本消息内容,最长不超过2048个字节,超过将截断)所以分四条发送

脚本定时发送微博热搜关键词到微信企业号

url-utf8.py url编码解码脚本

#!/bin/python
# -*- coding: utf-8 -*-
import urllib
import sys
rawurl = str(sys.argv[1])
url = urllib.unquote(rawurl)
print url

num.txt 发送消息每条消息取多少行

1,12p
13,25p
26,37p
38,50p

wxweibo.py 发送文本内容到企业微信应用的脚本

#!/usr/bin/python
#_*_coding:utf-8 _*_
__author__ = 'lvnian'
 
import urllib,urllib2
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')
 
def gettoken(corpid,corpsecret):
    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
    try:
        token_file = urllib2.urlopen(gettoken_url)
    except urllib2.HTTPError as e:
        print e.code
        print e.read().decode("utf8")
        sys.exit()
    token_data = token_file.read().decode('utf-8')
    token_json = json.loads(token_data)
    token_json.keys()
    token = token_json['access_token']
    return token
 
 
 
def senddata(access_token,user,content):
 
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
    send_values = {
        "touser":user ,    #企业号中的用户帐号,我这里按部门发送的,所以这个参数填错没关系。
        "toparty":"3",    #企业号中的部门id
        "msgtype":"text",  #发送的消息类型。
        "agentid":"1000003", #企业号中应用的AgentId
        "text":{
            "content":content
           },
        "safe":"0"
        }
    send_data = json.dumps(send_values, ensure_ascii=False)
    send_request = urllib2.Request(send_url, send_data)
    response = json.loads(urllib2.urlopen(send_request).read())
    print str(response)
 
if __name__ == '__main__':
    user = str(sys.argv[1])     #第一个参数(第一个和第二参数没实际作用,但不能为空)
    subject = str(sys.argv[2])  #第二个参数
    content = str(sys.argv[3])  #第三个参数(需要发送的消息内容)
    corpid =  '企业微信管理-我的企业-就可看到此id'   #CorpID是企业微信(原企业号)的标识
    corpsecret = '企业微信管理-应用管理-打开对应应用就可看到此参数'  #需要发送的应用Secret
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,content)

wxweibo.sh 执行脚本

#!/bin/bash
date=`date +%H:%M`

#下载热搜关键词前50页面并过滤热搜词和url
cd /tmp/weibo/ && wget -q https://s.weibo.com/top/summary/ 
grep "<a href=\"/weibo?" index.html|sed 's#^ *##;s/target="_blank"//'> /tmp/weibo/aa && cp aa ab

#对url编码解码并替换原来url
for i in `awk -F "[=|&]" '{print $3}' aa`
do
    wz="`python /tmp/weibo/url-utf8.py $i`"
    sed -i "s/$i/{$wz}/" ab
done

#删除和替换字符导出结果
sed 's/{//;s/}//;s#\/weibo#https://s.weibo.com#g;s/com/com\/weibo/g;s/#/%23/g;s/ //g;s/<a/<a /' ab >resou.txt


#分4次发送热搜词到公众号消息(1条消息限制最长2048个字节)
for i in `cat /tmp/weibo/num.txt`
do
    nub=`echo "$i"|sed 's/p//'|tr "," "-"`
    /bin/python /tmp/weibo/wxweibo.py 0 0 "[$date]微博热搜排名$nub关键词:
`sed -n $i /tmp/weibo/resou.txt`"
done

#删除临时文件
rm -rf index.html* aa ab 

wxweibo.sh 执行

sh /tmp/weibo/wxweibo.sh
{u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'}
{u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'}
{u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'}
{u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'}

crontab 每小时0分定时发送

00 * * * * /tmp/weibo/wxweibo.sh

百度搜热也可以这样做,效果展示;因为百度链接长所以分5条消息发送

脚本定时发送微博热搜关键词到微信企业号


本文最后更新时间 2024-03-30
文章链接地址:
https://me.jinchuang.org/archives/511.html
本站文章除注明[转载|引用],均为本站原创内容,转载前请注明出处
使用腾讯云CDN的SDK 用脚本去刷新URL缓存
统计文本中每个字符出现的次数
Nali 离线ip数据库文件
苹果CMS v10内容管理系统搭建

Protege-本体编辑开发工具

音频剪辑器 GoldWave

留言列表

  1. 许小逸
    Windows 10 Google Chrome
    中国天津市电信IDC机房

    你好,我想转载了该篇博文,用于论坛交流可以吗?

    1. Awking
      Windows 10 Google Chrome
      中国天津市联通

      个人娱乐用,可以转载的

      1. 许小逸
        Windows 10 Google Chrome
        中国天津市联通

        谢谢大佬!

我要留言