吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1066|回复: 17
收起左侧

[Python 原创] 使用selenium处理某音私信自动发送

[复制链接]
tianmenghuan 发表于 2024-4-12 08:40
本帖最后由 tianmenghuan 于 2024-4-16 14:02 编辑

[Python] 纯文本查看 复制代码
from tkinter import Tk, Label, Entry, Button
import time
from threading import Thread
import pygame
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

# --------------------本次思路是-------------------------------
# 1.先寻找新的消息数量找到就报警
# 2.找到后鼠标移动过去
# 3.在出现的聊天框里面再次寻找出现的新的消息数量,并点击
# 4.找到文本框元素并输入文本,最后点击发送
# 此项pass--不可使用留待以后学习更多知识之后看下能否完善
# --------------------------------------------------------------


# 设置浏览器的用户数据目录
chrome_options = Options()
chrome_options.add_argument(
    "--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default")

# 初始化 Chrome 浏览器
# 创建全局变量 driver
driver = webdriver.Chrome(options=chrome_options)


def wait_for_inputs():
    while not entry1.get() or not entry2.get():
        root.update_idletasks()  # 更新界面


# 用于等待的函数
def wait_for_element(driver0, xpath, timeout=30):
    try:
        element = WebDriverWait(driver, timeout).until(
            ec.presence_of_element_located((By.XPATH, xpath))
        )
        return element
    except TimeoutException:
        return None


def open_chrome():
    wait_for_inputs()  # 等待用户输入
    driver.get("https://www.douyin.com/user/self?showTab=post")  # 请将此处的网址替换为你要跳转的实际网址
    # time.sleep(40)


def run_code():
    """
    执行具体的操作
    """
    current_cycle = 0  # 初始化当前循环次数为 0
    for _ in range(int(entry2.get())):
        # 在循环中使用
        current_cycle += 1  # 在每次循环开始前增加循环次数
        root.title(f"抖音监控私信 - 第 {current_cycle} 次循环")  # 更新标题
        if current_cycle == int(entry2.get()):
            root.title("当前循环结束")
        try:
            # element = wait_for_element(driver,
            #                            "/html/body/div[2]/div/div[3]/div[1]/div[1]/header/div/div/div["
            #                            "2]/div/pace-island/div/ul[2]/div/li/div/div/div[3]/div/div/div[1]/div/div["
            #                            "2]/div/div[1]/div/div/div[3]/div")
            # 登录成功后的页面直接查询等待是否有新的信息出现
            element = wait_for_element(driver,
                                       "/html/body/div[2]/div/div[3]/div[1]/div[1]/header/div/div/div["
                                       "2]/div/pace-island/div/ul[2]/div/li/div/div/div[2]/div")
            if element:  # 如果出现新的信息
                # 元素找到,执行操作
                value = element.text
                print("元素的值:", value)
                # 在窗口上显示信息
                print("出现新的信息,开始执行警报")
                # 初始化 pygame 的混音器
                pygame.mixer.init()
                # 加载声音文件
                sound = pygame.mixer.Sound("D:/mp3/XX.wav")
                # 播放声音
                sound.play()
                pygame.time.wait(2000)  # 等待 2 秒
                # 停止音乐
                pygame.mixer.music.stop()
                tk_label.config(text="注意有新的信息出现,请及时查看")
                tk_label.after(10000, lambda: tk_label.config(text=""))
                # 将鼠标移动到元素位置并点击
                action = webdriver.ActionChains(driver)
                action.move_to_element(element).click().perform()
                # 查找点击信息后在聊天框出现的信息,这个是一定会找到的
                element = wait_for_element(driver,
                                           "/html/body/div[2]/div/div[3]/div[1]/div[1]/header/div/div/div["
                                           "2]/div/pace-island/div/ul[2]/div/li/div/div/div[3]/div/div/div[1]/div/div["
                                           "2]/div/div[1]/div/div/div[3]/div")

                action = webdriver.ActionChains(driver)
                action.move_to_element(element).click().perform()

                # 找到文本框 进行输入  文本后面+\n 这个功能不好,容易发送失败
                text_block = driver.find_element(By.CLASS_NAME, "public-DraftStyleDefault-block")
                text_block.send_keys(
                    "你好,世界")
                element0 = driver.find_element(By.CLASS_NAME, "sCp7KhBv.e2e-send-msg-btn")
                print("准备进行点击发送按钮")
                # 执行鼠标点击操作
                element0.click()

                # 点击退出会话
                element2 = driver.find_element(By.CLASS_NAME, "F2qAlnLO")
                element2.click()

                # # 等待用户输入的间隔时间
                time.sleep(int(entry1.get()))
            else:
                # 元素未找到,处理情况
                print("暂时没有新的消息,跳过")
                tk_label.config(text="暂时没有新的消息,跳过")
                tk_label.after(5000, lambda: tk_label.config(text=""))
        except NoSuchElementException as e:
            print(f"找不到元素:{e}")


def thread_runner():
    """
    在新线程中运行 run_code 函数
    """
    Thread(target=run_code).start()


root = Tk()
root.title("抖音监控私信")
root.geometry("250x110")  # 设置窗口大小为 300x100

tk_label = Label(root, text="结果展示", width=30, bd=10, bg="yellow")
tk_label.pack(pady=3)

# 创建第一个输入框和其上方的文字标签
label1 = Label(root, text="间隔时间:")
label1.pack()
label1.place(x=0, y=50)
entry1 = Entry(root, width=8)
entry1.pack()
entry1.place(x=60, y=50)
# 创建第二个输入框和其上方的文字标签
label2 = Label(root, text="循环次数:")
label2.pack()
label2.place(x=0, y=80)
entry2 = Entry(root, width=8)
entry2.pack()
entry2.place(x=60, y=80)


# 设置按钮的函数
def set_buttons():
    button2 = Button(root, text="登录", width=5, command=open_chrome)
    button2.pack(pady=10)
    button2.place(x=135, y=60)

    button = Button(root, text="运行", width=5, command=thread_runner)
    button.pack(pady=10)
    button.place(x=195, y=60)


set_buttons()
root.mainloop()


2024-4-16 更新处理陌生人消息并回关陌生人,有不足的地方欢迎各位大佬指点

[Python] 纯文本查看 复制代码
from tkinter import Tk, Label, Entry, Button
import time
from threading import Thread
import pygame
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

# --------------------本次思路是针对陌生人消息不显示私信数量的问题进行改进-------------------------------
# 1.先寻找私信图标,
# 2.找到后鼠标移动过去
# 方法一:
# 1.在出现的聊天框里面先寻找出现的新的消息数量,并点击
# 2.找到文本框元素并输入文本,最后点击发送
# 3.再下一轮寻找中如果没有新的私信信息 那就找陌生人消息
# 4.找到就点击一下 然后再发送文本
# 5.回关好友,再返回到聊天框

# 此项pass--不可使用留待以后学习更多知识之后看下能否完善
# --------------------------------------------------------------


# 设置浏览器的用户数据目录
chrome_options = Options()
chrome_options.add_argument(
    "--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default")

# 初始化 Chrome 浏览器
# 创建全局变量 driver
driver = webdriver.Chrome(options=chrome_options)


def wait_for_inputs():
    while not entry1.get() or not entry2.get():
        root.update_idletasks()  # 更新界面


# 用于等待的函数
def wait_for_element(driver0, xpath, timeout=10):
    try:
        element = WebDriverWait(driver, timeout).until(
            ec.presence_of_element_located((By.XPATH, xpath))
        )
        return element
    except TimeoutException:
        return None


def open_chrome():
    wait_for_inputs()  # 等待用户输入
    driver.get("https://www.douyin.com/user/self?showTab=post")  # 请将此处的网址替换为你要跳转的实际网址
    # time.sleep(40)


def run_code():
    """
    执行具体的操作
    """
    current_cycle = 0  # 初始化当前循环次数为 0
    for _ in range(int(entry2.get())):
        # 在循环中使用
        current_cycle += 1  # 在每次循环开始前增加循环次数
        root.title(f"抖音监控私信 - 第 {current_cycle} 次循环")  # 更新标题
        if current_cycle == int(entry2.get()):
            root.title("当前循环结束")
        try:

            # 登录成功后的页面直接查询等待是否有新的信息出现
            element = wait_for_element(driver,
                                       "/html/body/div[2]/div/div[3]/div[1]/div[1]/header/div/div/div["
                                       "2]/div/pace-island/div/ul[2]/div/li/div/div/div[2]/div")
            if element:  # 如果出现新的信息  元素找到,执行操作

                value = element.text
                print("元素的值:", value)
                # 在窗口上显示信息
                print("出现新的信息,开始执行警报")
                # 初始化 pygame 的混音器
                pygame.mixer.init()
                # 加载声音文件
                sound = pygame.mixer.Sound("D:/mp3/XX.wav")
                # 播放声音
                sound.play()
                pygame.time.wait(2000)  # 等待 2 秒
                # 停止音乐
                pygame.mixer.music.stop()
                tk_label.config(text="注意有新的信息出现,请及时查看")
                tk_label.after(10000, lambda: tk_label.config(text=""))
                # 将鼠标移动到元素位置并点击
                action = webdriver.ActionChains(driver)
                action.move_to_element(element).click().perform()
                # 查找点击信息后在聊天框出现的信息,这个是一定会找到的
                element = wait_for_element(driver,
                                           "/html/body/div[2]/div/div[3]/div[1]/div[1]/header/div/div/div["
                                           "2]/div/pace-island/div/ul[2]/div/li/div/div/div[3]/div/div/div[1]/div/div["
                                           "2]/div/div[1]/div/div/div[3]/div")

                action = webdriver.ActionChains(driver)
                action.move_to_element(element).click().perform()

                # 找到文本框 进行输入  文本后面+\n 这个功能不好,容易发送失败
                text_block = driver.find_element(By.CLASS_NAME, "public-DraftStyleDefault-block")
                text_block.send_keys(
                    "你好,详看个人资料")
                element0 = driver.find_element(By.CLASS_NAME, "sCp7KhBv.e2e-send-msg-btn")
                print("准备进行点击发送按钮")
                # 执行鼠标点击操作
                element0.click()

                # 点击退出会话
                element2 = driver.find_element(By.CLASS_NAME, "F2qAlnLO")
                element2.click()

                # # 等待用户输入的间隔时间
                time.sleep(int(entry1.get()))
            else:
                # 元素未找到,处理情况
                print("暂时没有新的私信数量消息,开始执行查找是否有新的陌生人消息")
                element = wait_for_element(driver,
                                           "//p[contains(text(), '私信')]")
                if element:  # 如果找到私信图标
                    # 元素找到,执行操作
                    print("找到私信图片,鼠标即将移动点击")
                    # 将鼠标移动到元素位置并点击
                    action = webdriver.ActionChains(driver)
                    action.move_to_element(element).click().perform()
                    time.sleep(2)
                    # 查看是否有陌生人新的消息
                    # element = wait_for_element(driver,
                    #                            "/html/body/div[2]/div/div[3]/div[1]/div[1]/header/div/div/div["
                    #                            "2]/div/pace-island/div/ul[2]/div/li/div/div/div[3]/div/div/div["
                    #                            "1]/div/div[2]/div/div[4]/div/div/div[1]/div")
                    element = driver.find_element(By.CLASS_NAME, "TQyd2bgK")  # TQyd2bgK陌生人图标元素
                if element:
                    print("找到陌生人图标,开始点击")
                    action = webdriver.ActionChains(driver)
                    action.move_to_element(element).click().perform()
                    time.sleep(5)
                    # tJjNB1rt ge6Vyp_V  陌生人消息数量元素
                    # //*[@id="island_b69f5"]/div/ul[2]/div/li/div/div/div[3]/div/div/div[1]/div/div[1]/div[1]
                    # JkInzlFQ 这个是陌生人返回按钮元素 上面的也是
                    element = driver.find_element(By.CLASS_NAME, "tJjNB1rt.ge6Vyp_V")  # tJjNB1rt ge6Vyp_V  陌生人消息数量元素

                if element:
                    print("出现新的陌生人信息,开始执行警报")
                    # 初始化 pygame 的混音器
                    pygame.mixer.init()
                    # 加载声音文件
                    sound = pygame.mixer.Sound("D:/mp3/XX.wav")
                    # 播放声音
                    sound.play()
                    pygame.time.wait(2000)  # 等待 2 秒
                    # 停止音乐
                    pygame.mixer.music.stop()
                    tk_label.config(text="注意有新的陌生人信息出现,请及时查看")
                    tk_label.after(10000, lambda: tk_label.config(text=""))
                    # 将鼠标移动到元素位置并点击
                    print("开始点击陌生人消息")
                    action = webdriver.ActionChains(driver)
                    action.move_to_element(element).click().perform()
                    # 找到文本框 进行输入  文本后面+\n 这个功能不好,容易发送失败
                    text_block = driver.find_element(By.CLASS_NAME, "public-DraftStyleDefault-block")
                    text_block.send_keys(
                        "你好,详看个人资料")
                    element0 = driver.find_element(By.CLASS_NAME, "sCp7KhBv.e2e-send-msg-btn")
                    print("准备进行点击发送按钮")
                    # 执行鼠标点击操作
                    element0.click()
                    # SHV6n6VV 回关元素
                    print("准备点击回关钮")
                    element = driver.find_element(By.CLASS_NAME, "SHV6n6VV") # SHV6n6VV 这个是陌生人回关按钮元素
                    action = webdriver.ActionChains(driver)
                    action.move_to_element(element).click().perform()
                    time.sleep(5)

                    print("操作完成,开始返回到正常聊天列表")
                    element = driver.find_element(By.CLASS_NAME, 'JkInzlFQ')  # JkInzlFQ 这个是陌生人返回按钮元素
                    print("开始点击返回按钮")
                    action = webdriver.ActionChains(driver)
                    action.move_to_element(element).click().perform()

                else:
                    print("暂时没有陌生人的消息,跳过")
                    tk_label.config(text="暂时没有陌生人的消息,跳过")
                    tk_label.after(5000, lambda: tk_label.config(text=""))
        except NoSuchElementException as e:
            print(f"找不到元素:{e}")


def thread_runner():
    """
    在新线程中运行 run_code 函数
    """
    Thread(target=run_code).start()


root = Tk()
root.title("抖音监控私信")
root.geometry("250x110")  # 设置窗口大小为 300x100

tk_label = Label(root, text="结果展示", width=30, bd=10, bg="yellow")
tk_label.pack(pady=3)

# 创建第一个输入框和其上方的文字标签
label1 = Label(root, text="间隔时间:")
label1.pack()
label1.place(x=0, y=50)
entry1 = Entry(root, width=8)
entry1.pack()
entry1.place(x=60, y=50)
# 创建第二个输入框和其上方的文字标签
label2 = Label(root, text="循环次数:")
label2.pack()
label2.place(x=0, y=80)
entry2 = Entry(root, width=8)
entry2.pack()
entry2.place(x=60, y=80)


# 设置按钮的函数
def set_buttons():
    button2 = Button(root, text="登录", width=5, command=open_chrome)
    button2.pack(pady=10)
    button2.place(x=135, y=60)

    button = Button(root, text="运行", width=5, command=thread_runner)
    button.pack(pady=10)
    button.place(x=195, y=60)


set_buttons()
root.mainloop()


免费评分

参与人数 4吾爱币 +10 热心值 +4 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
blindcat + 1 + 1 谢谢@Thanks!
woyucheng + 1 + 1 谢谢@Thanks!
notproblem + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

GVOPwZWhL2IFcIZ 发表于 2024-4-12 13:16
改用drissionpage吧
LiCan857 发表于 2024-4-16 10:49

drissionpage确实好用,但是我用的时候有个地方不知道是不是BUG,当点击出现新的tab的时候,打印所有tab的id,总是最多只有两个。如果停止程序,再次获取这个端口浏览器的tab,就能获取到所有的tab
smartfind 发表于 2024-4-12 09:04
zaibuo 发表于 2024-4-12 10:30
学习一哈
wushaolaiye 发表于 2024-4-12 10:48
会检测异常不
ying656 发表于 2024-4-12 10:51
持续关注这么有价值的帖子
LLLYH 发表于 2024-4-12 10:57
膜拜四大佬,持续关注
blindcat 发表于 2024-4-12 11:15
有点意思,学习一下
angelabebe 发表于 2024-4-12 12:56
感谢分享
Xiejinjie 发表于 2024-4-12 13:23
谢谢你,这个直得研究
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 19:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表