2016-07-29 19 views
0

我想抓取這個link的視頻名稱,因爲它是「瘋狂的女人瘋狂的人誰只是想要退款」。使用硒python3颳去視頻的標題

在網絡上的代碼是:

<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'

有任何錯誤?

回答

0

首先,find_elements_by_class_name() method返回一個列表WebElement s,而你需要一個單一的。此外,您還需要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 
+0

哇.. Booommm ......親愛達成...還剩3分鐘接受的答案。 – user6575792

+0

它也在工作。 'head = browser.find_elements_by_class_name('watch-title') 適合水溝: print(ditch.text)' – user6575792