Python 如何使用BeautifulSoup获取youtube评论

Python 如何使用BeautifulSoup获取youtube评论,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,我是个新手。我想知道如何使用BeautifulSoup从YouTube上抓取评论。我在这里被打动了。有人能帮我写代码吗 以下是我写的: import requests from bs4 import BeautifulSoup r = requests.get("https://www.youtube.com/watch?v=kffacxfA7G4" req =r.conten soup = BeautifulSoup(req,'html.parser') pr

我是个新手。我想知道如何使用BeautifulSoup从YouTube上抓取评论。我在这里被打动了。有人能帮我写代码吗

以下是我写的:

import requests    
from bs4 import BeautifulSoup

r = requests.get("https://www.youtube.com/watch?v=kffacxfA7G4"    
req =r.conten    
soup = BeautifulSoup(req,'html.parser')    
print(soup.prettify())    
all = soup.find_all('div',{'id' : 'contents'})

我被困在这里没有得到任何输出,检查wb页面,它显示评论id=contents

该站点的评论是动态生成的。使用
请求
美化组
库的主链接无法获取它们。要获得跟踪上述链接的内容,您需要使用任何浏览器模拟器,如
selenium
。作为初学者,您可以尝试以下方法。下面的脚本将获取未包装的注释。顺便说一句,该网站还启用了lazyloading方法,因此您需要对
for循环进行微调以获取更多内容

import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with Chrome() as driver:
    wait = WebDriverWait(driver,10)
    driver.get("https://www.youtube.com/watch?v=kffacxfA7G4")

    for item in range(3): #by increasing the highest range you can get more content
        wait.until(EC.visibility_of_element_located((By.TAG_NAME, "body"))).send_keys(Keys.END)
        time.sleep(3)

    for comment in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#comment #content-text"))):
        print(comment.text)
部分输出:

2018年4月15日?
2018年4月??

8年来,人们是为了学习
美丽群舞
,还是需要与Youtube互动?你可以使用Youtube的API-Hi,@Robert Seaman。我正在学习
BeautifulSoup
,并试图让Youtube的评论付诸实践。我觉得很难做到这一点。你能帮我吗?这里有一些更简单的页面可以用来学习
BeautifulSoup
。喜欢youtube的网站通常通过javascript加载页面,因此,如果你只使用
requests.get
,你不会看到太多数据。也许可以尝试使用
w3schools.com
example.com
?另外,请格式化您的代码,此代码段无法通过复制粘贴运行,因为它不完整