如何获得 YouTube 视频上传日期?
How can I get a YouTube video upload date?
我一直在尝试在 Python 中使用 Selenium 获取 YouTube 视频(如下图所示)的上传日期。
我的代码如下:
! pip install selenium
from selenium import webdriver
! pip install beautifulsoup4
from bs4 import BeautifulSoup
driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
driver.get("https://www.youtube.com/watch?v=CoR72I5BGUU")
soup = BeautifulSoup(driver.page_source, 'lxml')
upload_date=driver.find_elements_by_xpath('//*[@id="info-strings"]/yt-formatted-string')
print(upload_date)
但是,无论我用upload_date=soup.findAll('span', class_='style-scope ytd-grid-video-renderer')
还是upload_date=driver.find_elements_by_xpath('//*[@id="info-strings"]/yt-formatted-string')
,都没有用。
谁知道问题出在哪里,请帮帮我。
你可以试试下面的CSS_SELECTOR:
span#dot+yt-formatted-string[class$='ytd-video-primary-info-renderer']
代码:
upload_date = driver.find_element_by_css_selector("span#dot+yt-formatted-string[class$='ytd-video-primary-info-renderer']")
print(upload_date.text)
要获取“日期”,您可以使用 CSS select 或 #info-strings yt-formatted-string
,这将 select HTML ID info-strings
然后是 info-strings
标签。
此外,您应该等待页面正确加载,然后再尝试使用 WebDriverWait
抓取“日期”(需要额外导入,请参阅代码示例)。
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
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=CoR72I5BGUU")
# Waits 20 seconds for the "date" the appear on the webpage
date = WebDriverWait(driver, 20).until(
EC.visibility_of_element_located(
(By.CSS_SELECTOR, "#info-strings yt-formatted-string")
)
)
print(date.text)
driver.quit()
输出:
Jan 25, 2018
另请参阅:
- Selenium - wait until element is present, visible and interactable
我一直在尝试在 Python 中使用 Selenium 获取 YouTube 视频(如下图所示)的上传日期。
我的代码如下:
! pip install selenium
from selenium import webdriver
! pip install beautifulsoup4
from bs4 import BeautifulSoup
driver = webdriver.Chrome('D:\chromedrive\chromedriver.exe')
driver.get("https://www.youtube.com/watch?v=CoR72I5BGUU")
soup = BeautifulSoup(driver.page_source, 'lxml')
upload_date=driver.find_elements_by_xpath('//*[@id="info-strings"]/yt-formatted-string')
print(upload_date)
但是,无论我用upload_date=soup.findAll('span', class_='style-scope ytd-grid-video-renderer')
还是upload_date=driver.find_elements_by_xpath('//*[@id="info-strings"]/yt-formatted-string')
,都没有用。
谁知道问题出在哪里,请帮帮我。
你可以试试下面的CSS_SELECTOR:
span#dot+yt-formatted-string[class$='ytd-video-primary-info-renderer']
代码:
upload_date = driver.find_element_by_css_selector("span#dot+yt-formatted-string[class$='ytd-video-primary-info-renderer']")
print(upload_date.text)
要获取“日期”,您可以使用 CSS select 或 #info-strings yt-formatted-string
,这将 select HTML ID info-strings
然后是 info-strings
标签。
此外,您应该等待页面正确加载,然后再尝试使用 WebDriverWait
抓取“日期”(需要额外导入,请参阅代码示例)。
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
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=CoR72I5BGUU")
# Waits 20 seconds for the "date" the appear on the webpage
date = WebDriverWait(driver, 20).until(
EC.visibility_of_element_located(
(By.CSS_SELECTOR, "#info-strings yt-formatted-string")
)
)
print(date.text)
driver.quit()
输出:
Jan 25, 2018
另请参阅:
- Selenium - wait until element is present, visible and interactable