首页 > 解决方案 > Python Bot Twitch 查看器(Selenium)

问题描述

所以基本上我正在编写一个 python 脚本,它登录到一个 twitch 帐户并留在那里以生成一个查看器。

但我的主要问题是如何使这项工作适用于多个帐户。

如何隐藏所有 Windows,以及如何处理多个 selenium 窗口?

硒甚至对此有好处还是有其他方法?

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--mute-audio")
driver = webdriver.Chrome("D:\Downloads\chromedriver_win32\chromedriver.exe", chrome_options=chrome_options)


driver.minimize_window()

driver.get('https://www.twitch.tv/login')
search_form = driver.find_element_by_id('login-username')
search_form.send_keys('user')
search_form = driver.find_element_by_id('password-input')
search_form.send_keys('password')
search_form.submit()
driver.implicitly_wait(10)
driver.get('https://www.twitch.tv/channel')

标签: pythonseleniumbotstwitch

解决方案


你绝对可以使用 Selenium 和 Python 来做到这一点。要运行多个帐户,您将不得不利用多线程或创建多个驱动程序对象来管理。

线程的多线程示例:

from selenium import webdriver
import threading
import time

def test_logic():
    driver = webdriver.Firefox()
    url = 'https://www.google.co.in'
    driver.get(url)
    # Implement your test logic
    time.sleep(2)
    driver.quit()

N = 5   # Number of browsers to spawn
thread_list = list()

# Start test
for i in range(N):
    t = threading.Thread(name='Test {}'.format(i), target=test_logic)
    t.start()
    time.sleep(1)
    print t.name + ' started!'
    thread_list.append(t)

# Wait for all thre<ads to complete
for thread in thread_list:
    thread.join()

print 'Test completed!'

每个驱动程序都必须使用代理连接在不同的 IP 地址上连接到 Twitch。我建议使用 Opera,因为它有一个内置的 VPN,使它更容易。

线程中的 Opera 和 Selenium 示例:

from selenium import webdriver
from time import sleep

# The profile directory which Opera VPN was enabled manually using the GUI
opera_profile = '/home/user-directory/.config/opera' 
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + opera_profile)
driver = webdriver.Opera(options=options)
driver.get('https://whatismyipaddress.com')
sleep(10)
driver.quit()

要隐藏 webdrivers 的控制台,您必须使用“headless”选项运行它们。 无头 chrome 驱动程序

from selenium import webdriver from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")

不幸的是,Opera 驱动程序不支持 headless,因此您必须使用 Chrome 或 Firefox。

祝你好运!


推荐阅读