虎牙直播自动发送d幕,使用selenium *** 作网页自动发送d幕,因为虎牙限制,10秒发送一条。
温馨提示:需要电脑上安装Chrome浏览器
运行截图 使用方法: 代码启动(python3环境):- 在web.py启动服务
- 双击template下的index.html在浏览器中启动
- 点击初始化检查电脑环境,如果环境没有问题
登录虎牙
按钮变为可用 - 点击登录虎牙,d出虎牙官网扫码登录页面,使用手机虎牙app扫码登录(30秒内,页面会自动关闭)
- 点击检查登录状态,发送d幕前需要检查登录状态,否则不能发送。
需要重新点击登录并扫码登录
- 在左侧文本框中输入发送内容,程序随机挑选发送d幕
- 右侧发送条数中输入想要发送条数
- 点击开始发送,自动打开虎牙界面发送d幕(因为虎牙有限制,所以10秒发送一条)
- 点击初始化检查电脑环境,如果环境没有问题
- 双击dist目录下的web.exe文件启动服务
- 双击template下的index.html在浏览器中启动
- 点击初始化检查电脑环境,如果环境没有问题
登录虎牙
按钮变为可用 - 点击登录虎牙,d出虎牙官网扫码登录页面,使用手机虎牙app扫码登录(30秒内,页面会自动关闭)
- 点击检查登录状态,发送d幕前需要检查登录状态,否则不能发送。
需要重新点击登录并扫码登录
- 在左侧文本框中输入发送内容,程序随机挑选发送d幕
- 右侧发送条数中输入想要发送条数
- 点击开始发送,自动打开虎牙界面发送d幕(因为虎牙有限制,所以10秒发送一条)
- 点击初始化检查电脑环境,如果环境没有问题
目前系统自动在c盘创建文件夹huyaWorkSpace
目录,下载需要启动资源。
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')
# 点击电脑图标d出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
# 发送d幕消息
def send(msg, driver):
print('填入d幕内容'.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()
联系我:QQ2039808149
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)