我曾尝试使用BeautifulSoup刮取yt格式的字符串,但它总是给我一个错误。这是我的密码:
import requests
import bs4
from bs4 import BeautifulSoup
r = requests.get('https://www.youtube.com/channel/UCPyMcv4yIDfETZXoJms1XFA')
soup = bs4.BeautifulSoup(r.text, "html.parser")
def onoroff():
onoroff = soup.find('yt-formatted-string',{'id','subscriber-count'}).text
return onoroff
print("Subscribers: "+str(onoroff().strip()))
这就是我得到的错误
AttributeError: 'NoneType' object has no attribute 'text'
有没有其他方法可以刮取yt格式的字符串
大部分Youtube内容都是通过JavaScript生成的,这是BeautifulSoup所不具备的功能,但您可以通过在源代码中删除json对象,而不是直接删除HTML元素来获得运气,即:
Demo
相关问题 更多 >
编程相关推荐