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

安装

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 recity_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.5self.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=0for new in news:index+=1messages+= f"[{index}]标题:"+new[0]messages+= "\n"messages += "🔗:"+new[1]messages += "\n"return messagesdef 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 textdef 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'] + 1text = 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:continueTops10 = list(zip(title, a_url))[:10]messages = "【新浪微博热搜top10】\n 更新时间:" + str(datetime.datetime.now()).split('.')[:-1][0] + '\n'index = 0for top in Tops10:index += 1messages += f"[{index}]标题:" + top[0]messages += "\n"messages += "🔗:" + top[1]messages += "\n"return messages
if __name__ == '__main__':s = SendMsg()s.send_msg()

本文链接:https://my.lmcjl.com/post/12695.html

展开阅读全文

4 评论

留下您的评论.