本文實例為大家分享了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、搜索好友時,沒有辨別輸入的是例如聊天內容等其他東西,所以需要用戶確保自己輸入的是好友名
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 易語言修改快捷方式進行微信多開的代碼
- 利用Python讀取微信朋友圈的多種方法總結
- Android實現微信登錄的示例代碼
- Flutter實現微信朋友圈功能
- 今天辦公室海王小姐姐悄悄問我怎么在PC端登錄多個微信號