【问题标题】:youtube page scroll down using selenium, pythonyoutube页面使用selenium向下滚动,python
【发布时间】:2020-12-28 14:09:05
【问题描述】:
url_main = "https://www.youtube.com/feed/history"

我想在上面的网页中无限向下滚动,直到所有内容都可见,所以我尝试了

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time

driver = webdriver.Chrome(driverpath)
wait = WebDriverWait(driver, 10)


driver.get(url_main)
login = driver.find_element_by_css_selector('a[href="https://accounts.google.com/ServiceLogin?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Dko%26next%3Dhttps%253A%252F%252Fwww.youtube.com%252Ffeed%252Fhistory&hl=ko&ec=65620"]')
login.click()
login = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[type="email"]')))
login.send_keys(email)
login.send_keys(Keys.RETURN)
login = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[type="password"]')))
login.send_keys(password)
login.send_keys(Keys.RETURN)

content = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'div[id="primary"] div[id="contents"]')))

no_of_pagedowns = 1

while no_of_pagedowns:
    content.send_keys(Keys.PAGE_DOWN)
    time.sleep(1)
    no_of_pagedowns-=1

但不工作。我还尝试将代码复制粘贴到this link,但它也根本不起作用。

【问题讨论】:

  • 尝试将此添加到您的脚本for i in range (50): #scroll 1000px driver.execute_script('window.scrollTo(0,(window.pageYOffset+1000))') sleep(5) 您可以将 50 更改为任何可以解决问题的数字

标签: python selenium youtube selenium-chromedriver


【解决方案1】:

你可以这样实现:

# save the max scrollHeight of the page
scrollHeight = d.execute_script("return window.scrollMaxY")
scrolled_pages = 0
# while we have not reached the max scrollHeight
while d.execute_script("return window.pageYOffset") < scrollHeight:
# scroll one page down
    d.execute_script("window.scrollByPages(1)")
    scrolled_pages += 1
# wait to load any lazy loaded images (may not be needed, depending on your usecase)
    time.sleep(0.2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多