【发布时间】:2020-11-25 15:54:11
【问题描述】:
我的代码大部分都很好
我目前从 youtube 页面获取所有标题 + 滚动。
我如何获得观看次数?
CSS 或 xPath 会起作用吗?
import time
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
driver = webdriver.Chrome(ChromeDriverManager().install())
url='https://www.youtube.com/user/OakDice/videos'
driver.get(url)
last_height = driver.execute_script("return document.documentElement.scrollHeight")
SCROLL_PAUSE_TIME = 2
while True:
# Scroll down to bottom
time.sleep(2)
driver.execute_script("window.scrollTo(0, arguments[0]);", last_height)
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.documentElement.scrollHeight")
if new_height == last_height:
break
last_height = new_height
content=driver.page_source.encode('utf-8').strip()
soup=BeautifulSoup(content,'lxml')
titles = soup.findAll('a', id='video-title')
for title in titles:
print(title.text)
【问题讨论】:
标签: python selenium xpath css-selectors