【问题标题】:How to web scrape from youtube using Selenium and Python如何使用 Selenium 和 Python 从 youtube 抓取网页
【发布时间】:2020-07-30 15:59:50
【问题描述】:

代码trilas:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import json
options = Options()
options.headless = False
driver = webdriver.Chrome(options=options) //used to choose options from chrome//
driver.implicitly_wait(5)
baseurl = 'http://youtube.com'
keyword = input() #user input as earth
driver.get(f'{baseurl}/search?q= {keyword}')

我想从http://youtube.com网站抓取数据

【问题讨论】:

  • ...好吗?你已经向我们展示了一些代码。你有问题吗?请阅读How to Ask
  • 请告诉我们您想从本网站获取哪些数据,我们会尽力为您提供帮助。
  • 我想使用python从youtube获取链接,喜欢,不喜欢,观看次数数据
  • 我正在使用 jupyter 它显示错误,因为我使用 chrome 连接服务器

标签: python selenium web-scraping youtube user-input


【解决方案1】:

要使用Selenium 提取youtube 搜索结果的标题,您必须为visibility_of_all_elements_located() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    baseurl = "http://youtube.com"
    keyword = input()
    driver.get(f'{baseurl}/search?q={keyword}')
    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//yt-formatted-string[@class='style-scope ytd-video-renderer' and @aria-label]")))])
    driver.quit()
    
  • 控制台输出:

    earth
    ['Lil Dicky - Earth (Official Music Video)', 'The History of Earth - How Our Planet Formed - Full Documentary HD', "EARTH FROM SPACE: Like You've Never Seen Before", 'Lil Dicky - Earth (Lyrics)', 'Michael Jackson - Earth Song (Official Video)', 'Lil Dicky - Earth (CLEAN CENSORED VERSION)', 'Marshmello ft. Bastille - Happier (Official Music Video)', 'USA for Africa - We are the World', 'Lil Dicky - Freaky Friday feat. Chris Brown (Official Music Video)', 'What if Pluto Hits The Earth?', "15 Places on Earth Where Gravity Doesn't Seem to Work", 'Earth 101 | National Geographic', 'How Earth Moves', 'History Of Earth In 9 Minutes', 'What Happens If 1 mm Black Hole Appears On Earth?', 'Planet Earth seen from space (Full HD 1080p) ORIGINAL']
    

【讨论】:

  • WebDriverException:消息:“chromedriver.exe”可执行文件需要在 PATH 中。请参阅sites.google.com/a/chromium.org/chromedriver/home {在 jupyter 上运行此代码时出现此错误}
  • @KavitaNegi 这是一个完全不同的问题。您需要根据您的系统将 chromedriver 的路径从 C:\WebDrivers\chromedriver.exe 更改为值。否则,您可以针对您的新要求提出新问题。
  • 文件“”,第 10 行 baseurl = 'youtube.com' ^ SyntaxError: 无效语法
  • @KavitaNegi 将单引号替换为双引号,请重新测试并告知状态。
  • 相同的错误文件“”,第 10 行 baseurl =“youtube.com”^ SyntaxError:无效语法
猜你喜欢
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多