python - 当我在 YouTube 上添加评论时,它不会出现 python selenium
问题描述
我使用以下代码在YouTube上添加评论但是当我添加评论时,刷新页面后它没有出现。但是当我在我的个人浏览器中发表评论时,评论正常显示我认为谷歌知道我使用的是非个人浏览器所以它不显示评论是否有解决问题的想法?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium_stealth import stealth
import time
import os
import pickle
urlVideo = str(input('Enter your URL video: '))
import io
with io.open('message.txt', "r", encoding="utf-8") as f:
message = f.readlines()
options = webdriver.ChromeOptions()
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options)
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
driver.get("https://www.youtube.com/")
#input('login and click start')
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.get(urlVideo)
#click comments button
while True:
try:
driver.find_element_by_xpath('/html/body').send_keys(Keys.PAGE_DOWN)
time.sleep(0.5)
driver.find_element_by_xpath('//*[@id="placeholder-area"]').click()
driver.find_element_by_xpath('//*[@id="contenteditable-root"]').send_keys(message)
break
except:
pass
time.sleep(2)
while True:
try:
driver.find_element_by_css_selector('.style-scope.ytd-button-renderer.style-primary.size-default').click()
break
except:
pass
input('ok')
解决方案
Youtube 检测使用多个机器人标志,例如代理分数、硒检测、用户代理、过去的帐户活动等。你需要绕过所有这些来欺骗 youtube!我为客户制作了一个 youtube 机器人,为此我做了很多测试,这就是我知道这一点的原因。你需要让你的 chrome 驱动程序无法被检测到,并且表现得像人而不是机器人。
推荐阅读
- c - 来自 Windows 10 命令提示符的程序集-c
- docker - 从 Jenkins docker 容器连接到 localhost 从节点
- java - 将输入 \t 返回为 * 的程序
- android - 如何将 Json 数组转换为 List
? - css - 背景图像上的 CSS 曲线
- android-studio - 如何在 TabLayout 内的片段中更新 RecyclerView
- forms - 如何恢复最小化的模态形式?
- reactjs - 如何让第一个孩子使用 JSS-Nested
- jenkins - 如何使用 JenkinsPipelineUnit 模拟自定义步骤?
- javascript - 如何使垂直导航默认状态为折叠?