本文實例為大家分享了python半自動化發(fā)送微信信息的具體代碼,供大家參考,具體內容如下
相關第三方庫
1.pyautogui
自動操作鼠標、鍵盤的第三方庫
2.pyperclip
用于將文本復制和粘貼到剪貼板
3.requests
HTTP第三方庫
4.psutil
可以查看系統(tǒng)信息,進程、CPU等
5.騰訊地圖API
因為我想實現(xiàn)發(fā)送定位,所以需要用
總體思路
1.先手動登錄微信
2.使用os模塊調用微信進程
3.使用pyautogui模塊來自動操作微信的快捷鍵,實現(xiàn)搜索好友、發(fā)送信息,pyperclip模塊用來復制需要發(fā)送的中文
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() # 發(fā)送內容 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、發(fā)送定位,發(fā)送的只能是一個鏈接,沒有實現(xiàn)手機微信定位所實現(xiàn)的可預覽的效果
2、搜索好友時,沒有辨別輸入的是例如聊天內容等其他東西,所以需要用戶確保自己輸入的是好友名
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。