首页 > 解决方案 > 网页抓取 Youtube 页面

问题描述

我正在尝试通过链接在网络上抓取 youtube 频道名称。但我得到错误代码:

title = response.find_all('div', class_= "style-scope ytd-channel-name")
AttributeError: 'Response' object has no attribute 'find_all'

网站链接:https: //www.youtube.com/channel/UCHOgE8XeaCjlgvH0t01fVZg

代码:

url = 'https://www.youtube.com/channel/UCHOgE8XeaCjlgvH0t01fVZg'
response = requests.get(url)

title = response.find_all('div', class_= "style-scope ytd-channel-name")
soup = BeautifulSoup(title.text, 'lxml')
print(soup)

谢谢!

标签: pythonhtmlpython-3.xweb-scrapingyoutube

解决方案


我们可以使用这个。

from requests_html import HTMLSession
from bs4 import BeautifulSoup as bs # importing BeautifulSoup


video_url = "https://www.youtube.com/channel/UCHOgE8XeaCjlgvH0t01fVZg"
# init an HTML Session
session = HTMLSession()
# get the html content
response = session.get(video_url)
# execute Java-script
response.html.render(sleep=1)
# create bs object to parse HTML
soup = bs(response.html.html, "html.parser")
name = soup.find('yt-formatted-string', class_='style-scope ytd-channel-name')
print(name.text)

输出:-

TheTekkitRealm

推荐阅读