【问题标题】:Scrape heading of a video using selenium python3使用 selenium python3 抓取视频的标题
【发布时间】:2016-07-29 17:03:42
【问题描述】:

我想刮掉这个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' 对象没有属性 'text'

有什么问题吗?

【问题讨论】:

    标签: python python-3.x selenium web-crawler


    【解决方案1】:

    首先,find_elements_by_class_name() method返回WebElements 的列表,而您需要一个列表。另外,你需要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
    

    【讨论】:

    • 哇...... Booommm......到达亲爱的......接受答案还剩3分钟。
    • 它也在工作。 head = browser.find_elements_by_class_name('watch-title') for ditch in head: print(ditch.text)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多