Python半自动化发送微信信息

相关第三方库

  1. pyautogui
    自动操作鼠标、键盘的第三方库
  2. pyperclip
    用于将文本复制和粘贴到剪贴板
  3. requests
    HTTP第三方库
  4. psutil
    可以查看系统信息,进程、CPU等
  5. 腾讯地图API
    因为我想实现发送定位,所以需要用

总体思路

  1. 先手动登录微信
  2. 使用os模块调用微信进程
  3. 使用pyautogui模块来自动操作微信的快捷键,实现搜索好友、发送信息,pyperclip模块用来复制需要发送的中文
  4. 使用requests模块来调用腾讯地图的API

具体代码

# -*- coding: utf-8 -*
import sys

import psutil
import pyautogui
import time
import os
import pyperclip
import json
import requests


def main():
    # 先确认是否登录微信
    confirms = pyautogui.confirm("微信已登录?", "请先登录微信!")
    if confirms == "Cancel":
        sys.exit()
    get_wechat()
    find_friend()
    msg = locate()
    # 发送内容
    send(msg)


# 定位
def locate():
    url = "https://apis.map.qq.com/ws/location/v1/ip?key=自己申请的key"
    session = requests.Session()
    # 取消代理
    session.trust_env = False
    resp = session.get(url)
    print(resp.json())
    adress = resp.json()
    print(adress["result"]["location"])
    # 获取到经纬度
    point = adress["result"]["location"]
    # 由经纬度获取定位
    pointUrl = "https://apis.map.qq.com/uri/v1/geocoder?coord=" + str(point["lat"]) + "," + str(
        point["lng"]) + "&referer=自己申请的key"
    print(pointUrl)
    return pointUrl


def find_friend():
    pyautogui.hotkey("ctrl", "f")
    pyautogui.hotkey("ctrl", "a")
    pyautogui.hotkey("delete")
    content = pyautogui.prompt("请输入好友名:")
    if content is None:
        sys.exit()
    pyperclip.copy(content)
    pyautogui.hotkey("ctrl", "v")
    pyautogui.hotkey("enter")


def send(msg):
    pyperclip.copy(msg)
    pyautogui.hotkey("ctrl", "v")
    pyautogui.hotkey("enter")


# 查找进程
def get_wechat():
    flag = False
    pids = psutil.process_iter()
    for p in pids:
        if p.name() == "WeChat.exe":
            flag = True
            print(p.name())
            print(p.exe())
            os.system(p.exe())
            break
        else:
            continue
    if not flag:
        pyautogui.alert("请先登录微信!")


if __name__ == '__main__':
    pyautogui.FAILSAFE = True
    pyautogui.PAUSE = 0.2
    main()

不足之处

  1. 发送定位,发送的只能是一个链接,没有实现手机微信定位所实现的可预览的效果
  2. 搜索好友时,没有辨别输入的是例如聊天内容等其他东西,所以需要用户确保自己输入的是好友名

若汝可完善上述不足,望告之

	推广一下自己的公众号:软件优享
	公众号会收录一些优秀、新奇的软件、工具、PPT模板、电子资料等资源,并可提供下载。

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值