本文實(shí)例為大家分享了python半自動(dòng)化發(fā)送微信信息的具體代碼,供大家參考,具體內(nèi)容如下
相關(guān)第三方庫(kù)
1.pyautogui
自動(dòng)操作鼠標(biāo)、鍵盤的第三方庫(kù)
2.pyperclip
用于將文本復(fù)制和粘貼到剪貼板
3.requests
HTTP第三方庫(kù)
4.psutil
可以查看系統(tǒng)信息,進(jìn)程、CPU等
5.騰訊地圖API
因?yàn)槲蚁雽?shí)現(xiàn)發(fā)送定位,所以需要用
總體思路
1.先手動(dòng)登錄微信
2.使用os模塊調(diào)用微信進(jìn)程
3.使用pyautogui模塊來(lái)自動(dòng)操作微信的快捷鍵,實(shí)現(xiàn)搜索好友、發(fā)送信息,pyperclip模塊用來(lái)復(fù)制需要發(fā)送的中文
4.使用requests模塊來(lái)調(diào)用騰訊地圖的API
具體代碼
# -*- coding: utf-8 -*
import sys
import psutil
import pyautogui
import time
import os
import pyperclip
import json
import requests
def main():
# 先確認(rèn)是否登錄微信
confirms = pyautogui.confirm("微信已登錄?", "請(qǐng)先登錄微信!")
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=自己申請(qǐng)的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=自己申請(qǐng)的key"
print(pointUrl)
return pointUrl
def find_friend():
pyautogui.hotkey("ctrl", "f")
pyautogui.hotkey("ctrl", "a")
pyautogui.hotkey("delete")
content = pyautogui.prompt("請(qǐng)輸入好友名:")
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")
# 查找進(jìn)程
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("請(qǐng)先登錄微信!")
if __name__ == '__main__':
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.2
main()
不足之處
1、發(fā)送定位,發(fā)送的只能是一個(gè)鏈接,沒(méi)有實(shí)現(xiàn)手機(jī)微信定位所實(shí)現(xiàn)的可預(yù)覽的效果
2、搜索好友時(shí),沒(méi)有辨別輸入的是例如聊天內(nèi)容等其他東西,所以需要用戶確保自己輸入的是好友名
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 易語(yǔ)言修改快捷方式進(jìn)行微信多開(kāi)的代碼
- 利用Python讀取微信朋友圈的多種方法總結(jié)
- Android實(shí)現(xiàn)微信登錄的示例代碼
- Flutter實(shí)現(xiàn)微信朋友圈功能
- 今天辦公室海王小姐姐悄悄問(wèn)我怎么在PC端登錄多個(gè)微信號(hào)