Warm tip: This article is reproduced from serverfault.com, please click

python-网站抓取YouTube页面

(python - Webscraping Youtube pages)

发布于 2020-11-28 08:32:53

我正在尝试通过链接通过网络抓取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)

谢谢!

Questioner
Kab2k
Viewed
0
Abhishek Rai 2020-11-29 18:08:56

我们可以使用它。

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