python 能过selenium跟微信交互操作2
·
截图及识别
主要想通过识别微信聊天截图的文字,在触发关键字时,程序自动到网页去查询内容,然后通过键盘的模拟,将查询的结果发送到微主聊天中。

# -*- coding: UTF-8 -*-
import json
import string
import sys
import time
import pyautogui
import pyperclip
import requests
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import re
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
from bs4 import BeautifulSoup
chrome_service = Service(ChromeDriverManager().install())
ocr = PaddleOCR(use_angle_cls=True, lang="ch")
SEND_NAMES = ['aaa','bb','fptxyy', 'cc', 'ee', 'ff-Young','ss']
DELAY_TIME = 2
HELP = """使用方法 fo 问题或关键字
"""
# 发送微信 用copy C copy V 等实现
def send_wx_message(target, msg):
try:
pyperclip.copy("")
pyperclip.copy(target)
pyautogui.hotkey("ctrl", "f")
time.sleep(DELAY_TIME)
pyautogui.hotkey("ctrl", "v")
pyautogui.press("enter")
time.sleep(DELAY_TIME)
pyperclip.copy("")
pyperclip.copy(msg)
pyautogui.hotkey("ctrl", "v")
time.sleep(DELAY_TIME)
pyautogui.press("enter")
print("发送微信消息")
im = pyautogui.screenshot()
im.save('screenshot.png')
except Exception as ex:
print("发送微信消息出现异常: " + str(ex))
sys.exit(0)
# 识别图片加入换行
def ocr_img(language, img_path, result_img):
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False, lang=language)
img_path = img_path
result = ocr.ocr(img_path, cls=True)
worlds = "\n".join(line[-1][0] for line in result)
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores)
im_show = Image.fromarray(im_show)
im_show.save(result_img)
return worlds
#输出帮助
def get_help(words):
poem = HELP
return poem
#淨空老法師影音圖文檢索中心
def get_fa(question):
if question == "fo":
return ""
print('get_fa', question)
if '问题或关键字' in question:
return ""
options = webdriver.ChromeOptions()
options.add_argument('--no-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--headless')
chrome_service = Service(ChromeDriverManager().install())
# Use the Service object when creating the WebDriver
driver = webdriver.Chrome(service=chrome_service, options=options)
driver.get('https://ft.amtb.cn')
# Find the input element with ID "query" on the page
input_element = driver.find_element(by=webdriver.common.by.By.ID, value="query")
input_element.send_keys(question)
# Find the search button with ID "searchButton" on the page
search_button = driver.find_element(by=webdriver.common.by.By.ID, value="searchButton")
search_button.click()
sleep(10)
# Wait for the results to load
# Get the updated page source after the search
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
try:
article = soup.find('article')
text = article.get_text()
# Use regex to extract Chinese characters
chinese_characters = re.sub('<[^<]+?>', '', text)
driver.quit()
return chinese_characters
except:
return "无"
# 截图 用用户名进行区分
def cut_screen(name):
pyperclip.copy("")
pyperclip.copy(name)
pyautogui.hotkey("ctrl", "f")
time.sleep(2)
pyautogui.hotkey("ctrl", "v")
pyautogui.press("enter")
time.sleep(2)
pyperclip.copy("")
time.sleep(2)
screenshot = pyautogui.screenshot(region=(259, 64, 703, 637))
screenshot.save(f"./pic/{name}screenshot_region.png")
# Process OCR results for different cases
def process_ocr_results(name, text):
process_ocr_results_by_keyword(name, text, 'help', '操作说明', get_help)
print('操作说明 finished')
sleep(2)
process_ocr_results_by_keyword(name, text, 'fo', '淨空老法師中心', get_fa)
print('淨空老法師中心 finished')
sleep(2)
def remove_prefix(s, prefix):
if s.startswith(prefix):
return s[len(prefix):]
return s
# Process OCR results for a specific keyword
def process_ocr_results_by_keyword(name, text, keyword, txtform, service_func):
shiname = f"{name}_{keyword}"
if text!="" and keyword=="help" :
pattern = re.compile(rf'{keyword}')
matches = [i for i in pattern.findall(text) if i]
else:
pattern = re.compile(rf'{keyword}(.*)')
matches = [i for i in pattern.findall(text) if i]
if matches:
result = matches[-1].strip()
if result != '':
last_result = last_question.get(shiname, "")
if last_result != result:
last_question[shiname] = result
if '问题或关键字' not in result:
return_world: string = service_func(result)
if return_world != "":
send_wx_message(name, return_world + f"\n{txtform}")
time.sleep(DELAY_TIME)
if __name__ == '__main__':
last_question = {}
while True:
for name in SEND_NAMES:
print(name)
cut_screen(name)
language = 'ch'
img_path = f"./pic/{name}screenshot_region.png"
result_img = f"./pic/{name}screenshot_region_out.png"
text = ocr_img(language, img_path, result_img)
process_ocr_results(name, text)
time.sleep(5)
更多推荐


所有评论(0)