使用 selenium python3 抓取视频的标题
Scrape heading of a video using selenium python3
我想抓取这个 link 的视频名称,因为它是“疯狂的女人对只想退款的男人发疯”。
网络上的代码是:
<span id="eow-title" class="watch-title" dir="ltr" title="Insane Woman Goes Crazy On Guy Who Just Wants A Refund">
Insane Woman Goes Crazy On Guy Who Just Wants A Refund
我是这样做的:
browser = webdriver.Firefox()
browser.get("https://www.youtube.com/watch?v=POk-uOQSJVk")
head = browser.find_elements_by_class_name('watch-title')
print(head.text)
提示为:
AttributeError: 'list' object has no attribute 'text'
有没有错?
首先,find_elements_by_class_name()
method returns a list of WebElement
s, while you need a single one. Also, you need to let the page load until the desired element is present:
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=POk-uOQSJVk")
# wait for the presence of the video title
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "eow-title"))
)
print(element.text)
browser.close()
打印:
Insane Woman Goes Crazy On Guy Who Just Wants A Refund
我想抓取这个 link 的视频名称,因为它是“疯狂的女人对只想退款的男人发疯”。
网络上的代码是:
<span id="eow-title" class="watch-title" dir="ltr" title="Insane Woman Goes Crazy On Guy Who Just Wants A Refund">
Insane Woman Goes Crazy On Guy Who Just Wants A Refund
我是这样做的:
browser = webdriver.Firefox()
browser.get("https://www.youtube.com/watch?v=POk-uOQSJVk")
head = browser.find_elements_by_class_name('watch-title')
print(head.text)
提示为:
AttributeError: 'list' object has no attribute 'text'
有没有错?
首先,find_elements_by_class_name()
method returns a list of WebElement
s, while you need a single one. Also, you need to let the page load until the desired element is present:
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=POk-uOQSJVk")
# wait for the presence of the video title
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "eow-title"))
)
print(element.text)
browser.close()
打印:
Insane Woman Goes Crazy On Guy Who Just Wants A Refund