【问题标题】:Youtube Webscraping Comments by newYoutube Webscraping Comments by new
【发布时间】:2020-11-30 20:43:51
【问题描述】:

我正在尝试通过网络抓取 youtube 评论部分。但在此之前,我想按 new 对 cme​​ts 进行排序。所以我必须点击 SORT BY 然后 Newest First。但不幸的是,我没有运气。感谢您的帮助。

屏幕记录:https://imgur.com/Rt8gnIB

代码:

import sys, unittest, time, datetime
import urllib.request, urllib.error, urllib.parse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import InvalidArgumentException
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains
from bs4 import BeautifulSoup
import requests
import requests.exceptions
from urllib.parse import urlsplit
from collections import deque
import re
import os
import shutil
import smtplib
import string
import pyautogui

options = webdriver.ChromeOptions()
options.add_argument('--lang=en')
options.add_argument("--start-maximized")

driver = webdriver.Chrome(executable_path=r'C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Email\chromeDriver\chromedriver.exe', chrome_options=options)
driver.get("https://www.youtube.com/watch?v=EV6PLN_8RBw")
time.sleep(5)

screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()

pyautogui.moveTo(1050, 780) # Move the mouse to XY coordinates.
pyautogui.click()
print("clicked")
time.sleep(1)

pyautogui.moveTo(1250, 880) # Move the mouse to XY coordinates.
pyautogui.click() 
print("clicked")
time.sleep(1)

pyautogui.moveTo(700, 500) # Move the mouse to XY coordinates.
pyautogui.click() 
print("clicked")
time.sleep(1)

【问题讨论】:

  • ...必须点击 SORT BY,然后点击 Newest First...,在哪一行没有运气?
  • 不幸的是两者
  • 我尝试同时使用 XPath 和完整 XPath,但在站点上找不到元素。 (最后两行)

标签: python python-3.x selenium selenium-webdriver pyautogui


【解决方案1】:

我强烈建议先替换 autogui 部分 element = driver.find_element_by_xpath 或通过 css_selector 和 element.click 从“最新”选项中选择 xpath/css_selector 并再次单击后,我在另一个脚本中执行此操作并且没有问题,我无法正确格式化它,因为我在 android 上

tl;dr:通过单击 selenium 替换 autogui,首先是使选项可选择的按钮,然后再次单击要对其进行排序的选项

编辑:好吧,我自己试过了,pyautogui 对我不起作用,不确定是不是因为我使用了另一个驱动程序或其他什么,但如果它对你有用,那很好。问题是,您需要先向下滚动一点,才能加载 cmets。你可以使用

for i in range(5):
driver.execute_script("arguments[0].scrollBy(0, 500)", element)
time.sleep(2)

不确定 5 是否足够/太多,因为我无法检查自己,但基本上就是这样。然后,您可以使用

找到下拉打开按钮
    element = driver.find_element_by_xpath("//*[@id="label"]")
    element.click
    #now we select the option from the dropdown options
    element = driver.find_element_by_xpath("/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[1]/span/yt-sort-filter-sub-menu-renderer/yt-dropdown-menu/paper-menu-button/iron-dropdown/div/div/paper-listbox/a[2]/paper-item")  
    element.click

您也可以使用explicitWaits 来确保它在视图中,但我觉得使用它还不方便,您可以找到它们here
试试看,让我知道它是否有效:D

【讨论】:

  • 我使用 autogui 的原因是因为在谷歌页面上弹出的接受条款不允许我点击 xpath。如果你以后有时间,你能把如何点击排序和最新的代码发给我吗?我尝试同时使用 xpath 和完整的 xpath,不幸的是它找不到路径。
  • 对于弹出窗口,您可能需要 autogui,我在另一个问题中看到了该问题,但尚未解决。如果它首先有效,我将不得不尝试自己,但我在它上面^^
  • 目前正在写答案,可能我不确定窗口是否要最大化
  • 很抱歉听到这个消息,您可以尝试使用 css_selector 或使用另一个按钮,因为定义了 3 个不同的按钮,我不确定要使用哪个,我建议尝试并且有点错误
  • 我想我当时没有想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 2023-02-06
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
相关资源
最近更新 更多