首页 > 编程学习 > 微信发送新闻每日汇报

微信发送新闻每日汇报

发布时间:2022/9/23 4:22:06

运行代码的时候要打开微信o

# coding=utf8
import pyautogui
import pyperclip
import time
import requests
from lxml import etree


def get_requests():
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
    html = requests.get('https://www.163.com/dy/media/T1603594732083.html', headers=headers)
    tree = etree.HTML(html.text)
    src = tree.xpath('//div[@class="desc"]//a/@href')
    html = requests.get(src[0], headers=headers)
    tree = etree.HTML(html.text)
    contents = tree.xpath('//p[@id][1]/text()')
    return contents


def open_txt():
    with open('./daily_report.txt', 'w') as f:
        text = get_requests()
        # print(type(str(text[0])))
        for i in text:
            article = str(i)
            f.write(article)
            f.write('\n')


def get_msg():
    try:
        open_txt()
    except:
        print("")
    """发送文本里的信息"""
    with open('./daily_report.txt') as f:
        contents = f.read()
    f.close()
    return contents


def send(msg):
    # 复制需要发送的内容到粘贴板
    pyperclip.copy(msg)
    # 模拟键盘 ctrl + v 粘贴内容
    pyautogui.hotkey('ctrl', 'v')
    # 发送消息
    pyautogui.press('enter')


def send_msg(friend, m):
    if m == 1:
        # Ctrl + alt + w 打开微信
        pyautogui.hotkey('ctrl', 'alt', 'w')
    # 搜索好友
    pyautogui.hotkey('ctrl', 'f')
    # 复制好友昵称到粘贴板
    pyperclip.copy(friend)
    # 模拟键盘 ctrl + v 粘贴
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(1)
    # 回车进入好友消息界面
    pyautogui.press('enter')
    # 发送消息
    send(get_msg())


def send_friends():
    m = 1
    for i in friend_name:
        send_msg(i, m)
        time.sleep(1)
        m = m + 1
 
 
if __name__ == '__main__':
    # 填写发送好友的名字,用'\n'隔开,可发送多个人
    friend_name = '文件传输助手' \
                  '\nMiYol'
    friend_name = friend_name.split('\n')
    send_friends()
Copyright © 2010-2022 mfbz.cn 版权所有 |关于我们| 联系方式|豫ICP备15888888号