python - 网页抓取 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)
谢谢!
解决方案
我们可以使用这个。
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
推荐阅读
- javascript - ReactJS map() onMouseEnter 将css添加到特定索引
- string - 我正在尝试将输入字符串与存储在 var 中的字符串进行比较。但它不工作
- python - 使用“in”语句检查列表中的多个值之一
- java - 在 Java 14 中在运行时将 JarFile 添加到 Classpath
- android - 未解决的`kotlin-parcelize`
- google-cloud-platform - GCP gsutil set cors:没有这样的文件或目录
- css - 页面外的 React Navbar 下拉菜单
- api - 使用 XML 和 Javascript 的 USPS 地址验证 API
- javascript - 当我打开当前目标时,如何在组件的所有实例中隐藏 div
- wordpress - 在 Wordpress 上为网站的所有页面设置特色图片