如何通过 Selenium 和 Python 使用用户代理点击 YouTube 评论中的 link
How to click a link within youtube comment using an user agent through Selenium and Python
我正在编写一个脚本来点击 YouTube 评论中的 link,它工作正常,但是当我将它与用户代理结合使用时它不起作用,有人可以帮助我吗?
示例:
link
Html :
<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/redirect?redir_token=ix1uBK3TO3bxXdcT7EvDFp-vI9p8MTU0NjQxNTExOEAxNTQ2MzI4NzE4&event=comments&q=https%3A%2F%2Fjulissars.itworks.com%2F&stzid=Ugw6ip_QkzwyJPIq3bp4AaABAg" rel="nofollow">https://julissars.itworks.com</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()
Google 非常积极地阻止机器人与其大部分属性进行交互。如果您发送的用户代理字符串确实是随机的(也就是说,它与浏览器可能发送的真实 UA 不对应),您可能会陷入他们的机器人检测算法中。
因此,从某种意义上说,您的点击可能是 "working",因为它实际上在网络浏览器中触发了一个事件...但是 Google 可能忽略了请求。
链接的网页不支持所有浏览器(用户代理)。如果这是你的问题,你可以选择一些支持的用户代理并在它们之间轮换(例如 random.choice()
)。您还需要更新您的 xpath。
from selenium import webdriver
from fake_useragent import UserAgent
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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")
xpath = '/html/body/div[2]/div[4]/div/div[5]/div[2]/div[2]/div/div[2]/div[5]/div/div[2]/section[1]/div[1]/div[2]/div[2]/div[1]/a'
try:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath)))
finally:
element.click()
要单击文本为 https://julissars.itworks.com within the url 的所需评论,您需要诱导 WebDriverwait 以使元素可单击,您可以使用以下解决方案 useragent 通过 Selenium 和 Python:
代码块:
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
您可以在
中找到详细的讨论
我正在编写一个脚本来点击 YouTube 评论中的 link,它工作正常,但是当我将它与用户代理结合使用时它不起作用,有人可以帮助我吗?
示例: link
Html :
<a class="yt-simple-endpoint style-scope yt-formatted-string" spellcheck="false" href="/redirect?redir_token=ix1uBK3TO3bxXdcT7EvDFp-vI9p8MTU0NjQxNTExOEAxNTQ2MzI4NzE4&event=comments&q=https%3A%2F%2Fjulissars.itworks.com%2F&stzid=Ugw6ip_QkzwyJPIq3bp4AaABAg" rel="nofollow">https://julissars.itworks.com</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()
Google 非常积极地阻止机器人与其大部分属性进行交互。如果您发送的用户代理字符串确实是随机的(也就是说,它与浏览器可能发送的真实 UA 不对应),您可能会陷入他们的机器人检测算法中。
因此,从某种意义上说,您的点击可能是 "working",因为它实际上在网络浏览器中触发了一个事件...但是 Google 可能忽略了请求。
链接的网页不支持所有浏览器(用户代理)。如果这是你的问题,你可以选择一些支持的用户代理并在它们之间轮换(例如 random.choice()
)。您还需要更新您的 xpath。
from selenium import webdriver
from fake_useragent import UserAgent
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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")
xpath = '/html/body/div[2]/div[4]/div/div[5]/div[2]/div[2]/div/div[2]/div[5]/div/div[2]/section[1]/div[1]/div[2]/div[2]/div[1]/a'
try:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xpath)))
finally:
element.click()
要单击文本为 https://julissars.itworks.com within the url 的所需评论,您需要诱导 WebDriverwait 以使元素可单击,您可以使用以下解决方案 useragent 通过 Selenium 和 Python:
代码块:
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
您可以在
中找到详细的讨论