抖音点赞和公屏检测回复,不完整程序

发布时间 2023-10-28 18:21:59作者: 万花技术
from selenium import webdriver
import time
import pickle
edge = webdriver.Edge()
edge.maximize_window()
# 设置最大等待时长为 10秒
edge.implicitly_wait(10)
edge.get('https://www.douyin.com/')
time.sleep(1)
input("登入抖音账号后,请输入任意键继续...")
time.sleep(0.3)
with open("ph.pickle",'wb') as file:
    pickle.dump(edge.get_cookies(), file)
edge.delete_all_cookies()
time.sleep(1)
edge.quit()
使用以上代码保存cookie


使用以下代码工作
from selenium import webdriver
import time
import pickle
import random
import pyautogui
import threading
from selenium.webdriver.common.by import By


def main():
    # 新创建线程
    s1 = threading.Thread(target=job1, name="T1")
    s2 = threading.Thread(target=job2, name="T1")
    # 启动新线程
    s1.start()
    s2.start()


def job1():
    # 进入直播间先点赞300次
    for i in range(300):
        a = random.randrange(300, 550)
        b = random.randrange(400, 550)
        pyautogui.click(a, b)
        time.sleep(0.2)

    arr0 = ([["大家戳戳屏幕点点关注,点点赞,主播需要你们的支持哦"], ["亮一个抖币\
    可以加群哦,群里面有高清手稿分享"], ["主播现在写的字是形楷,喜欢的可以加群跟着练哦"], ["来吧,大家把赞点起来!"]])
    for j in range(100):
        wartime = random.randrange(120, 180)
        time.sleep(wartime)
        while True:
            textElement = edge.find_element(By.XPATH,
                                            '//textarea[@class="webcast-chatroom___textarea"]')
            textElement.clear()
            textElement.send_keys(random.choice(arr0[random.randrange(0, 3)]))  # 输入新字符串
            time.sleep(0.5)
            sendElement = edge.find_element(By.XPATH,
                                            '//button[@class="webcast-chatroom___send-btn"][@type="button"]')
            time.sleep(1)
            sendElement.click()
            break
         
def job2():
    # 第二线程,关键字检测消息回复
    # 提取公屏的最后一个发言,和数组比对,如果能匹配就发送对应的消息
    # 直到浏览器退出
    arrT1 = ([["9390"], ["80g木浆纸"], ["主播使用的垫子在橱窗有哦"]])
    var = 1
    while var == 1:
        WebText = edge.find_elements(By.XPATH, '//a[contains(text())]')  # 可能这里还有点小问题,查找的文本应该不准确,或者差参数
        match WebText:
            case "笔":
                while True:
                    textElement = edge.find_element(By.XPATH,
                                                    '//textarea[@class="webcast-chatroom___textarea"]')
                    textElement.clear()
                    textElement.send_keys(random.choice(arrT1[0]))  # 输入新字符串
                    time.sleep(0.5)
                    sendElement = edge.find_element(By.XPATH,
                                                    '//button[@class="webcast-chatroom___send-btn"][@type="button"]')
                    time.sleep(1)
                    sendElement.click()
                    break
            case "纸":
                while True:
                    textElement = edge.find_element(By.XPATH,
                                                    '//textarea[@class="webcast-chatroom___textarea"]')
                    textElement.clear()
                    textElement.send_keys(random.choice(arrT1[1]))  # 输入新字符串
                    time.sleep(0.5)
                    sendElement = edge.find_element(By.XPATH,
                                                    '//button[@class="webcast-chatroom___send-btn"][@type="button"]')
                    time.sleep(1)
                    sendElement.click()
                    break
            case "垫":
                while True:
                    textElement = edge.find_element(By.XPATH,
                                                    '//textarea[@class="webcast-chatroom___textarea"]')
                    textElement.clear()
                    textElement.send_keys(random.choice(arrT1[2]))  # 输入新字符串
                    time.sleep(0.5)
                    sendElement = edge.find_element(By.XPATH,
                                                    '//button[@class="webcast-chatroom___send-btn"][@type="button"]')
                    time.sleep(1)
                    sendElement.click()
                    break


if __name__ == '__main__':
    with open("ph.pickle", 'rb') as file:
        cookiesList = pickle.load(file)
    edge = webdriver.Edge()
    edge.maximize_window()
    edge.get('https://www.douyin.com/')
    for cookie in cookiesList:
        edge.add_cookie(cookie)
    # 自己修改本行满足自己的直播间
    edge.get('https://live.douyin.com/143865879903')
    time.sleep(10)
    main()[/md]