Huya automatically sends barrage

introduce

Huya Live automatically sends barrages, and uses selenium to operate web pages to automatically send barrages. Because of Huya's limitations, one message is sent in 10 seconds.

Reminder: Chrome browser needs to be installed on the computer

run screenshot

insert image description here

insert image description here

insert image description here

Instructions:

Code to start (python3 environment):

  1. Start the service in web.py
  2. Double-click index.html under template to start it in the browser
    • Click Initialize to check the computer environment, if there is no problem with the environment 登录虎牙button becomes available
    • Click to log in to Huya, and the scan code login page of Huya's official website will pop up, and use the mobile phone Huya app to scan the code to log in (within 30 seconds, the page will automatically close)
    • Click Check Login Status, you need to check the login status before sending the popup, otherwise it cannot be sent. You need to click login again and scan the code to log in
    • Enter the content to send in the text box on the left, and the program randomly selects and sends the barrage
    • Enter the number of pieces you want to send in the number of pieces to send on the right
    • Click to start sending, automatically open the Huya interface to send the barrage (because Huya has restrictions, so send one in 10 seconds)

exe start

  1. Double-click the web.exe file in the dist directory to start the service
  2. Double-click index.html under template to start it in the browser
    • Click Initialize to check the computer environment, if there is no problem with the environment 登录虎牙button becomes available
    • Click to log in to Huya, and the scan code login page of Huya's official website will pop up, and use the mobile phone Huya app to scan the code to log in (within 30 seconds, the page will automatically close)
    • Click Check Login Status, you need to check the login status before sending the popup, otherwise it cannot be sent. You need to click login again and scan the code to log in
    • Enter the content to send in the text box on the left, and the program randomly selects and sends the barrage
    • Enter the number of pieces you want to send in the number of pieces to send on the right
    • Click to start sending, automatically open the Huya interface to send the barrage (because Huya has restrictions, so send one in 10 seconds)

working environment

At present, the system automatically creates a folder huyaWorkSpacedirectory on the c drive, and the download needs to start the resource.

Reference Code

from cgi import test
from lib2to3.pgen2 import driver
import re
from tkinter import E
from turtle import st
import requests
import time
import json
from selenium import webdriver
import sys
import getChromeVersion
import random


def testDriver(driverPath):

    try:
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
        driver = webdriver.Chrome(
            options=options, executable_path=driverPath + '\\chromedriver.exe')
        driver.get("https://www.baidu.com/")
        title = driver.title
        if "百度" in title:
            return True
    except Exception:
        print(str("报错").center(50, '-'))
        return False
    finally:
        driver.close()
    return False


# 通过扫码登录


def loginByScan(WORK_SPACE):
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    driver = webdriver.Chrome(
        options=options, executable_path=WORK_SPACE + '\\chromedriver.exe')

    driver.get('https://i.huya.com/index.php?m=Subscribe&watch=1')
    driver.implicitly_wait(10)
    # 切换到iframe
    driver.switch_to.frame('UDBSdkLgn_iframe')

    driver.implicitly_wait(10)
    time.sleep(20)

    driver.get('https://www.huya.com/967722')
    # 获取cookie
    cookies = driver.get_cookies()
    # print(cookies)
    # cookie保存到cookies.txt文件
    f1 = open(WORK_SPACE + "\\cookies.txt", "w")
    json.dumps(cookies)
    f1.write(str(json.dumps(cookies)))
    f1.close()

    driver.close()

# 检查登录状态


def checkLoginState(WORK_SPACE):
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    driver = webdriver.Chrome(
        options=options, executable_path=WORK_SPACE + '\\chromedriver.exe')

    driver.get("https://www.huya.com/967722")
    # 加载cookie
    cookies = readCookies()
    for cook in cookies:
        driver.add_cookie(cook)

    driver.refresh()
    time.sleep(2)
    # 检测是否为登录状态
    text = driver.find_element(
        by='xpath', value='/html/body/div[2]/div/div/div[1]/div[2]/div[2]/div/div[3]/div[1]/div[6]/div[3]').get_attribute('innerHTML')
    
    driver.close()
    if '登录' in text:
        return '请先登录!'
    else:
        return '1'


def login(user, password):
    try:
        options = webdriver.ChromeOptions()
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
        driver = webdriver.Chrome(
            options=options, executable_path=sys.path[0] + '\\chromedriver.exe')
        driver.get('https://i.huya.com/index.php?m=Subscribe&watch=1')
        # time.sleep(2)
        driver.implicitly_wait(10)
        # 切换到iframe
        driver.switch_to.frame('UDBSdkLgn_iframe')
        # 点击电脑图标弹出QQ登录选项
        driver.find_element(
            by='xpath', value='/html/body/div[1]/div[2]/div[1]/div[4]/i').click()
        driver.implicitly_wait(10)
        # 点击qq登录
        driver.find_element(
            by='xpath', value='/html/body/div[1]/div[1]/div[2]/div[2]/ul/li[2]').click()

        driver.implicitly_wait(10)
        # 切换窗口
        driver.switch_to.window(driver.window_handles[1])
        # 切换到iframe
        driver.switch_to.frame('ptlogin_iframe')

        driver.find_element(
            by='xpath', value='/html/body/div[1]/div[9]/a[1]').click()

        driver.implicitly_wait(10)
        # 输入账号密码
        driver.find_element(
            by='xpath', value='/html/body/div[1]/div[5]/div/div[1]/div[3]/form/div[1]/div/input').send_keys(user)
        driver.find_element(
            by='xpath', value='/html/body/div[1]/div[5]/div/div[1]/div[3]/form/div[2]/div[1]/input').send_keys(password)
        driver.find_element(
            by='xpath', value='/html/body/div[1]/div[5]/div/div[1]/div[3]/form/div[4]/a/input').click()

        driver.implicitly_wait(10)
        time.sleep(5)

        # 切换窗口
        driver.switch_to.window(driver.window_handles[0])
        # 访问指定网址
        driver.get('https://www.huya.com/967722')
        # 获取cookie
        cookies = driver.get_cookies()
        # print(cookies)
        # cookie保存到cookies.txt文件
        f1 = open(sys.path[0] + "/cookies.txt", "w")
        json.dumps(cookies)
        f1.write(str(json.dumps(cookies)))
        f1.close()

        driver.close()
    except Exception:
        print('获取cookie失败!'.center(50, '-'))


def sendNumMsg(msgList, count, WORK_SPACE):

    # 屏蔽没用日志
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    driver = webdriver.Chrome(
        options=options, executable_path=WORK_SPACE + '/chromedriver.exe')
    driver.get('https://www.huya.com')
    # 加载cookie
    driver.implicitly_wait(10)
    time.sleep(3)
    cookies = readCookies(WORK_SPACE=WORK_SPACE)
    # print(cookies)
    for cook in cookies:
        driver.add_cookie(cook)
    # 刷新页面

    driver.refresh()
    driver.get('https://www.huya.com/967722')
    # time.sleep(2)
    driver.implicitly_wait(10)

    for i in range(count):
        send(msg=str(msgList[random.randint(
            0, len(msgList) - 1)]), driver=driver)
        time.sleep(10)

    driver.close()
    return True


# 发送弹幕消息
def send(msg, driver):
    print('填入弹幕内容'.center(50, '-'))
    driver.find_element_by_xpath('//*[@id="pub_msg_input"]').click()
    driver.find_element_by_xpath('//*[@id="pub_msg_input"]').send_keys(msg)
    # time.sleep(5)
    driver.implicitly_wait(10)
    print('点击发送'.center(50, '-'))
    driver.find_element_by_xpath('//*[@id="msg_send_bt"]').click()
    # time.sleep(2)
    driver.implicitly_wait(10)
    # 获取文本内容
    text = driver.find_element_by_xpath(
        '//*[@id="chat-room__list"]').get_attribute('innerHTML')
    time.sleep(2)
    if msg in text:
        print('发送成功'.center(50, '-'))


# 读取本地cookie
def readCookies(WORK_SPACE):
    f = ''
    try:
        print('读取cookie文件'.center(50, '-'))
        f = open(WORK_SPACE+'\\cookies.txt', 'r')
        str = f.read()
        # print(str)
        # str.replace("\n", "")
        my_list = json.loads(str)
        print('读取cookie文件成功!'.center(50, '-'))
        return my_list
    except FileNotFoundError:
        print('没有cookie文件,请先扫码登录'.center(50, '-'))
    finally:
        if f:
            f.close()


if __name__ == '__main__':

    # loginByScan()

    msgList = ['666', '5sdgf']
    sendNumMsg(msgList=msgList, count=5, WORK_SPACE='C:\\huyaWorkSpace\\')
    print(random.randint(0, len(msgList) - 1))
    print(str(msgList[random.randint(0, len(msgList) - 1)]))

    # print(sys.path[0])
    # sys.path[0]
    # sendMsg('星魂666')
    # print(readCookies())
    # getCookies()

Contact me: QQ2039808149

Guess you like

Origin blog.csdn.net/weixin_43960044/article/details/124086953