【发布时间】:2018-07-08 16:31:44
【问题描述】:
我是一个使用python3 for windows的初学者。 我的问题是我试图从 youtube 播放列表中抓取标题和投票(喜欢/不喜欢),并且似乎无法让我的脚本等待下一页加载,然后再对下一页进行投票,直到播放列表结束。
相反,它只取标题,并且在复制所有内容后,第一页的投票重复此操作并仅单击下一页一次。
我用谷歌搜索并查看了其他帖子,认为可能需要调用显式等待,但它似乎仍然不起作用。
当前脚本:
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
browser = webdriver.Firefox()
browser.get(
'https://www.youtube.com/watch?v=2bnMiScBRfQ&list=PLx1Dr6w7DLoLfPixTug9c8xrTkGUsyhkQ&index=')
videosInPlaylist = []
for x in range(1, 4):
wait = WebDriverWait(browser, 10)
title = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'h1.title'))).text
positiveVotes = wait.until(EC.visibility_of_element_located(
(By.CSS_SELECTOR, 'ytd-toggle-button-renderer.style-text:nth-child(1) > a:nth-child(1) > yt-formatted-string:nth-child(2)'))).text
negativeVotes = wait.until(EC.visibility_of_element_located(
(By.CSS_SELECTOR, 'ytd-toggle-button-renderer.style-text:nth-child(2) > a:nth-child(1) > yt-formatted-string:nth-child(2)'))).text
currentVideo = [title, positiveVotes, negativeVotes]
nextVideo = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, 'ytd-playlist-panel-video-renderer.style-scope:nth-child(3) > a:nth-child(1) > div:nth-child(1) > div:nth-child(3)')))
videosInPlaylist.append(currentVideo)
nextVideo.click()
print(videosInPlaylist)
请帮忙。
【问题讨论】:
标签: python selenium selenium-webdriver youtube