gpt4 book ai didi

python - 如何通过 Selenium 和 Python 使用用户代理单击 youtube 评论中的链接

转载 作者:行者123 更新时间:2023-11-28 22:13:24 25 4
gpt4 key购买 nike

我正在编写一个脚本来点击 youtube 评论中的链接,它工作正常,但是当我将它与用户代理结合使用时它不起作用,有人可以帮助我吗?

例子: link

HTML :

<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/redirect?redir_token=ix1uBK3TO3bxXdcT7EvDFp-vI9p8MTU0NjQxNTExOEAxNTQ2MzI4NzE4&amp;event=comments&amp;q=https%3A%2F%2Fjulissars.itworks.com%2F&amp;stzid=Ugw6ip_QkzwyJPIq3bp4AaABAg" rel="nofollow">https://julissars.itworks.com&#65279;</a>

代码试用(没有用户代理,它可以工作):

from selenium import webdriver
from fake_useragent import UserAgent
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://www.youtube.com/watch?v=UJezMYvf8Ss&lc=Ugw6ip_QkzwyJPIq3bp4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and contains(., 'julissars')]"))).click()

代码试用(使用用户代理,它不起作用):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent

useragent = UserAgent()
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", useragent.random)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://www.youtube.com/watch?v=UJezMYvf8Ss&lc=Ugw6ip_QkzwyJPIq3bp4AaABAg")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-simple-endpoint style-scope yt-formatted-string' and contains(., 'julissars')]"))).click()

最佳答案

单击文本为 https://julissars.itworks.com 的所需评论在 url 内您需要诱导 WebDriverwait 以使元素可点击,您可以通过 SeleniumPython useragent 使用以下解决方案em>:

  • 代码块:

    from selenium import webdriver
    from fake_useragent import UserAgent
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

    ua = UserAgent()
    options = webdriver.ChromeOptions()
    userAgent = ua.random
    print(userAgent)
    options.add_argument('user-agent=' + userAgent)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.youtube.com/watch?v=UJezMYvf8Ss&lc=Ugw6ip_QkzwyJPIq3bp4AaABAg")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='yt-uix-sessionlink ' and contains(@href, 'julissars')]"))).click()
  • 控制台输出:

    Mozilla/5.0 (X11; NetBSD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
  • 您可以在 Way to change Google Chrome user agent in Selenium? 中找到详细的讨论。

关于python - 如何通过 Selenium 和 Python 使用用户代理单击 youtube 评论中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53993925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com