本文實例為大家分享了python半自動化發(fā)送微信信息的具體代碼,供大家參考,具體內(nèi)容如下
相關(guān)第三方庫
1.pyautogui
自動操作鼠標(biāo)、鍵盤的第三方庫
2.pyperclip
用于將文本復(fù)制和粘貼到剪貼板
3.requests
HTTP第三方庫
4.psutil
可以查看系統(tǒng)信息,進程、CPU等
5.騰訊地圖API
因為我想實現(xiàn)發(fā)送定位,所以需要用
總體思路
1.先手動登錄微信
2.使用os模塊調(diào)用微信進程
3.使用pyautogui模塊來自動操作微信的快捷鍵,實現(xiàn)搜索好友、發(fā)送信息,pyperclip模塊用來復(fù)制需要發(fā)送的中文
4.使用requests模塊來調(diào)用騰訊地圖的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()
# 發(fā)送內(nèi)容
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"])
# 獲取到經(jīng)緯度
point = adress["result"]["location"]
# 由經(jīng)緯度獲取定位
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、發(fā)送定位,發(fā)送的只能是一個鏈接,沒有實現(xiàn)手機微信定位所實現(xiàn)的可預(yù)覽的效果
2、搜索好友時,沒有辨別輸入的是例如聊天內(nèi)容等其他東西,所以需要用戶確保自己輸入的是好友名
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 易語言修改快捷方式進行微信多開的代碼
- 利用Python讀取微信朋友圈的多種方法總結(jié)
- Android實現(xiàn)微信登錄的示例代碼
- Flutter實現(xiàn)微信朋友圈功能
- 今天辦公室海王小姐姐悄悄問我怎么在PC端登錄多個微信號