吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5209|回复: 14
收起左侧

[Python 转载] python配合企业微信实现每日消息推送

[复制链接]
modys 发表于 2021-5-2 16:20
本帖最后由 modys 于 2021-5-2 17:30 编辑

前几天看了@Ysnv  https://www.52pojie.cn/thread-1338005-1-1.html大神的帖子,想到自己也写一个想看的东西推送一下,所以也用他的借口,真的很方便。
企业微信注册以及接口地址可以参照上面的帖子
自己写了天气+当地的疫情+度娘头条热点,挂到自己的服务器,每天定时发送一些内容。
20210502160028.jpg

20210502161018.jpg

[Python] 纯文本查看 复制代码
import requests, urllib, sys
from lxml import etree


class TopNews():
    def __init__(self):
        self.url = 'http://top.baidu.com/'
        self.headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
            }
    def get_url(self):
        r = requests.get(url=self.url, headers=self.headers)
        r.encoding = 'gb2312'
        tree = etree.HTML(r.text)
        li_list = tree.xpath('//*[@id="hot-list"]/li')
        titles =[]
        for li in li_list:
            num = li_list.index(li) + 1
            title = li.xpath('./a/@title')[0]
            titles.append(str(num) + ':' + title + '!')
        return titles


class Weather():
    def __init__(self):
        self.data={
            "location":"自己的城市", 
            "key":"自己的key"
        }

    def get_url(self):
        # API地址用S6,也可以用V7
        data_list = requests.get('https://free-api.heweather.com/s6/weather/forecast',params=self.data).json()
        daily_forecast = data_list["HeWeather6"][0]["daily_forecast"]
        forecast_list = []
        for i in daily_forecast:
            # print(daily_forecast.index(i))
            forecast = {}
            # 取列表索引
            if daily_forecast.index(i) == 0:
                forecast['时间'] = '今天   '
            elif daily_forecast.index(i) == 1:
                forecast['时间'] = '明天   '
            else:
                forecast['时间'] = '后天   '
            all_forecast = forecast['时间'] + '  白天:'+ i['cond_txt_d'] + '  晚上:' + i['cond_txt_n'] + '  最高温度:'+ i['tmp_max'] + '°C' + '  最低温度:'+ i['tmp_min'] + '°C' + '  风力:' + i['wind_dir'] + i['wind_sc'] + '级' + '!'

            forecast_list.append(all_forecast)
        select_forecast = "".join(forecast_list)
        new_data = select_forecast.replace('!', '\n')
        return new_data


class YiQing:
    # 由urllib2改成requests
    def __init__(self):
        self.host = 'http://ncovdata.market.alicloudapi.com'
        self.path = '/ncov/cityDiseaseInfoWithTrend'
        self.method = 'GET'
        self.appcode = '自己的AppCode' 
        self.querys = ''
        self.bodys = {}
        self.url = self.host + self.path

        self.new_appcode = 'APPCODE ' + self.appcode
        self.headers = {
            'Authorization': self.new_appcode
        }
    def get_data(self):
        response = requests.get(url=self.url, headers=self.headers)
        content = response.text
        # with open('./yiqing.html', 'w', encoding='utf-8') as fp:
        #     fp.write(content)
        # 文本转换成字典
        dict_content = eval(content)
        # 从所有数据取出当地省份内容
        new_content = dict_content['provinceArray'][7]
        yiqing_data = '河南最新疫情:' + '\n' + '新增:' + str(new_content['totalDoubtful']) + '\n' + '累计:' + str(new_content['totalConfirmed']) + '\n' + '累计治愈:' + str(new_content['totalCured']) + '\n' + '累计死亡:' + str(new_content['totalDeath'])
        # print(yiqing_dat)
        return yiqing_data

class SendText:
    def __init__(self, newsdata, hewdata, yqdata):
        self.news_data = newsdata
        self.hew_data = hewdata
        self.yq_data = yqdata
        self.corpid = "自己的corpid"
        self.corpsecret = "自己的"
        self.agentid = "自己的"
    def server_j(self):
        text_content = "".join(self.news_data)
        new_content = text_content.replace('!', '\n')
        # print(new_content)
        send_data = str(self.hew_data) + '\n' + self.yq_data + '\n' + '\n' + new_content

        data = {
        "corpid":self.corpid,
        "corpsecret":self.corpsecret,
        "agentid":self.agentid,
        "text":send_data
                }
        req = requests.post('https://api.htm.fun/api/Wechat/text/',data=data)
        # print(req)
        return req
def main():
    news = TopNews()
    heweather = Weather()
    yiqing = YiQing()
    text = SendText(news.get_url(), heweather.get_url(), yiqing.get_data())
    # print(text.server_j())
if __name__ == '__main__':
    main()


代码写的很烂,大神略过,可以自己添加一些其他的内容发送

免费评分

参与人数 4吾爱币 +7 热心值 +4 收起 理由
guoxiujing + 1 用了楼主的代码,执行了什么都没有 也不报错 求解
ouwei_bt + 1 + 1 我很赞同!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
joneqm + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

东坡明 发表于 2021-10-18 16:54
class Weather():
    def __init__(self):
        self.data={
            "location":"自己的城市",
            "key":"自己的key"
        }
你好,请问这个key 是什么网址的key
 楼主| modys 发表于 2021-10-19 08:46
东坡明 发表于 2021-10-18 16:54
class Weather():
    def __init__(self):
        self.data={

和风天气开发平台
sghzlx 发表于 2021-5-2 16:39
hehehegfd 发表于 2021-5-2 16:45
谢谢分享!
jyguang 发表于 2021-5-2 16:53
很意识,谢谢分享
轩辕翔龙 发表于 2021-5-2 17:37
感谢分享,来学习一下
h3cie 发表于 2021-5-3 19:04

感谢分享,来学习一下
Hangjau 发表于 2021-9-16 13:13
大哥 我封装了一个专门的库 可以看下  https://www.52pojie.cn/forum.php ... ;extra=#pid40046686
东坡明 发表于 2021-10-19 17:15
modys 发表于 2021-10-19 08:46
和风天气开发平台

好的谢谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-8 21:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表