使用Python自动发送消息可以通过多种方式实现,具体取决于您想发送消息的平台。以下是几种常见的方法:
发送消息
```python
import pyautogui
import time
模拟组合键打开
gui.hotkey('ctrl', 'alt', 'z')
循环发送消息
for i in range(1, 100):
gui.typewrite(message='!') 发送的内容
gui.hotkey('ctrl', 'enter') 模拟组合键发送消息
time.sleep(1) 暂停1秒
发送微信消息
```python
from threading import Timer
from wxpy import Bot
连接微信
bot = Bot()
发送消息给自己
bot.self.send('测试')
定义发送新闻函数
def get_news():
url = 'http://open.iciba.com/dsapi/'
r = requests.get(url)
contents = r.json()['content']
translation = r.json()['translation']
return contents, translation
def send_news():
try:
my_friend = bot.friends().search('绝影神驹')
my_friend.send('今天又到了我给你发问候的时候,嘻嘻嘻\n')
my_friend.send(get_news())
my_friend.send(get_news()[13:])
except Exception as e:
print(e)
定时发送新闻
Timer(60 * 60, send_news).start() 每小时发送一次新闻
发送邮件
```python
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
设置邮箱信息
mail_host = 'smtp.163.com' 邮箱服务器地址
mail_sender = '' 发件人邮箱
mail_password = 'your_email_password' 邮箱授权码
mail_receivers = ['', ''] 收件人邮箱列表
创建邮件对象
msg = MIMEMultipart()
msg['From'] = Header(mail_sender, 'utf-8')
msg['To'] = Header(', '.join(mail_receivers), 'utf-8')
msg['Subject'] = Header('邮件主题', 'utf-8')
邮件正文
body = '邮件正文内容'
msg.attach(MIMEText(body, 'plain', 'utf-8'))
发送邮件
smtp = smtplib.SMTP_SSL(mail_host, 465) 使用SSL连接
smtp.login(mail_sender, mail_password) 登录邮箱
smtp.sendmail(mail_sender, mail_receivers, msg.as_string()) 发送邮件
smtp.quit() 断开连接
请根据您的具体需求选择合适的方法,并确保您已经了解了相关的隐私和安全措施。如果您需要发送中文消息,可能需要使用第三方库如`pyperclip`来复制和粘贴中文字符。