【问题标题】:failed to crawl youtube video title name未能抓取 youtube 视频标题名称
【发布时间】:2020-10-17 18:47:40
【问题描述】:

我试图爬取所有视频标题名称,但没有得到结果,并且出现了一堆错误,有人可以告诉我我制作的哪些部分是错误的

import requests
from bs4 import BeautifulSoup
import  operator

def start(url):
    word_list = []
    source_code = requests.get(url).text
    soup = BeautifulSoup(source_code,"html.parser")
    for post_text in soup.findAll('a',{'class':'yt-simple-endpoint style-scope ytd-grid-video- 
 renderer'}): 
 
        content = post_text.string
        words = content.lower().split()
        for each_word in words:
            print(each_word)
            word_list.append(each_word)

start('https://www.youtube.com/c/DolceVitaChannel/videos')

【问题讨论】:

    标签: python web-scraping beautifulsoup request


    【解决方案1】:

    我修复了您的代码中的一些问题,并使用标题再次尝试,但没有成功。所以我决定使用硒。然而,它奏效了。这是代码。

    from selenium import webdriver
    
    firefox_options = webdriver.FirefoxOptions()
    #firefox_options.add_argument('--headless') # uncomment to use headless mode
    
    driver = webdriver.Firefox(executable_path='geckodriver', firefox_options=firefox_options)
    
    def start(url):
        word_list = []
        driver.get(url)
        for post_text in driver.find_elements_by_id('video-title'): 
            content = post_text.text
            words = content.lower().split()
            for each_word in words:
                print(each_word)
                word_list.append(each_word)
    
    start('https://www.youtube.com/c/DolceVitaChannel/videos')
    driver.close()
    

    【讨论】:

    • 看起来像一组函数,而不是beautifulsoup和请求的东西,但是为什么这个beautifulsoup和请求不能工作,是那个网页有防火墙还是什么?
    • 据我所知是因为 JS
    【解决方案2】:

    当您想在 youtube 或 instagram 之类的网站上进行抓取时,您会遇到 javascript 代码的问题,这需要您同时在该网站上使用浏览器来抓取您想要的内容。

    但解决方案是:

    1 - 在这些情况下,您可以转到浏览器的网络部分并检查发送到服务器的请求并查找所需的响应(在这种情况下,我没有找到所需的内容)。

    2 - 如果 youtube 为您提供了您想要的 API,您可以使用它,或者您可以搜索更简单的方法并寻找可以与 youtube api 一起使用的 python 包,例如 steam 库 [您可以搜索,因为 python 只是没有图书馆可以让死人复活:))]

    3 - 我最喜欢的解决方案是使用强大的 selenium 框架。您既可以打开浏览器,也可以在不打开浏览器的情况下使用无头功能。

    你可以使用下面的代码,它是selenium框架和beautifulsoup库的结合

    from selenium import webdriver
    from bs4 import BeautifulSoup
    
    driver_path = r'geckodriver path'
    
    firefox_options = webdriver.FirefoxOptions()
    firefox_options.add_argument('--headless')
    
    driver = webdriver.Firefox(executable_path=driver_path, firefox_options=firefox_options)
    
    def start(url):
        titles_list = []
        driver.get(url)
        soup = BeautifulSoup(driver.page_source,"html.parser")
        posts_div = soup.find_all('div',{'class':'style-scope ytd-grid-renderer'})
    
        posts = posts_div[0].find_all("ytd-grid-video-renderer")
    
        for post in posts:
            details = post.find("div", attrs={"id": "details"})
            post_title = details.find("a").text
            titles_list.append(post_title)
        return titles_list
    
    
    words = start('https://www.youtube.com/c/DolceVitaChannel/videos')
    driver.close()
    
    
    print("\n\n++++++++++++++++++++++++++++++++++++++++\n\n")
    
    for word in words:
        print(word)
    

    【讨论】:

    • 谢谢你的回复,其实我没学过java,一般只是需要写一些java代码来修复它,是这样吗?还有那个无头是什么意思……
    • 正确的说法是你可以说 js 而不是 java,因为它们是不同的,而且你不需要知道如何使用 js 代码,你可以使用我说的解决方案而且那无头是 selenium 中的选项名称,当您希望爬虫运行得更快并且您不需要通过驱动程序打开系统浏览器并执行所需的操作时。
    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 2021-09-05
    • 2015-10-23
    相关资源
    最近更新 更多