python 微信for Mac 自动发送当前热搜和天气

该脚本使用Python的多个库实现自动化信息推送,包括网易新闻头条、微博热搜及天气预报。它首先从指定网站抓取新闻标题和链接,然后获取微博热搜榜,并结合城市代码获取天气信息。最后,通过模拟键盘操作将这些信息发送到微信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

pip3 install pyautogui -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install pyperclip -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install parsel -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip3 install requests -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

源码

import pprint
import time
import pyautogui as pg
import pyperclip as pc
import requests
import parsel
import datetime
import re

city_code={
            "中山市":101281701,
            "广州市":101280101,
            "东莞市": 101281601   
        }
class SendMsg(object):

    def __init__(self):
        self.name = '文件传输助手'
        self.msg = str(self.get_day_news())
        self.msg+="\n======================\n"
        self.msg+= str(self.weibo_hot())
        self.msg+="\n======================\n"
        self.msg+= str(self.get_weather("中山市"))

    def send_msg(self):
        # 操作间隔为1秒
        pg.PAUSE = 0.5
        self.show_or_hide_wechat()
        self.find_people()

        # 找到好友
        pc.copy(self.name)
        self.paste()
        pg.press('enter')
        # 发送消息
        pc.copy(self.msg)
        self.paste()
        pg.press('enter')

        # 隐藏微信
        time.sleep(1)
        self.show_or_hide_wechat()

    def show_or_hide_wechat(self):
        pg.keyDown('ctrl')
        pg.keyDown('command')
        pg.keyDown('w')

        pg.keyUp('w')
        pg.keyUp('command')
        pg.keyUp('ctrl')
    def find_people(self):
        pg.keyDown('command')
        pg.keyDown('f')
        pg.keyUp('f')
        pg.keyUp('command')
    def paste(self):
        pg.keyDown('command')
        pg.keyDown('v')
        pg.keyUp('v')
        pg.keyUp('command')

    def get_day_news(self):
        headers = {
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
        }
        res = requests.get('https://c.m.163.com/news/sub/T1348647909107.html', headers=headers)
        selector = parsel.Selector(res.text)
        news_title = selector.css('.news-title::text').getall()
        a_url = list('https:' + i for i in selector.css('.single-picture-news a::attr(href)').getall())
        news=list(zip(news_title[:10], a_url[:10]))
        messages="【最新网易头条前10】\n 更新时间:"+str(datetime.datetime.now()).split('.')[:-1][0]+'\n'
        index=0
        for new in news:
            index+=1
            messages+= f"[{index}]标题:"+new[0]
            messages+= "\n"
            messages += "🔗:"+new[1]
            messages += "\n"
        return messages

    def get_weather(self,cityName):
        value=city_code[cityName]
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
        # url链接
        url = f'http://t.weather.itboy.net/api/weather/city/{value}'
        res = requests.get(url=url, headers=headers)
        ymd = re.findall('"ymd":"(.*?)"', res.text)[1]  # 年月日
        week = re.findall('"week":"(.*?)"', res.text)[1]  # 星期
        high = re.findall('"high":"高温(.*?)"', res.text)[1]  # 最高温度
        low = re.findall('"low":"低温(.*?)"', res.text)[1]  # 最低温度
        type0 = re.findall('"type":"(.*?)"', res.text)[1]  # 天气类型
        notice = re.findall('"notice":"(.*?)"', res.text)[1]  # 提示
        # 拼接数据
        text = f"【{cityName}】\n明天:{ymd}-{week}\n天气:{type0}\n气温:{low}-{high}\n提示:{notice}\n"
        return text

    def weibo_hot(self):
        url = 'https://weibo.com/ajax/statuses/hot_band'
        res = requests.get(url)
        items = res.json()['data']['band_list'][:12]
        # print(items)
        title = []
        a_url = []
        for item in items:
            try:
                rank = item['rank'] + 1
                text = item['mblog']['text']
                title.append(parsel.Selector(text).css("a::text").getall()[0])
                a_url.append('https:' + re.findall("href=\"(//s\.weibo\.com/weibo.*?)\"", text)[0])
            except:
                continue
        Tops10 = list(zip(title, a_url))[:10]
        messages = "【新浪微博热搜top10】\n 更新时间:" + str(datetime.datetime.now()).split('.')[:-1][0] + '\n'
        index = 0
        for top in Tops10:
            index += 1
            messages += f"[{index}]标题:" + top[0]
            messages += "\n"
            messages += "🔗:" + top[1]
            messages += "\n"

        return messages
if __name__ == '__main__':
    s = SendMsg()
    s.send_msg()


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值