使用Python和BeautifulSoup获取YouTube点赞数
在网上找了很多关于用PHP获取YouTube的点赞和点踩数量的资料,但用Python的却没找到。我想用BeautifulSoup来抓取这些点赞和点踩的数量,因为YouTube的API里没有这些信息。
我知道点赞和点踩的数量是在这个标签里:
<span class="watch-likes-dislikes">
<span class="likes">6</span> likes, <span class="dislikes">0</span> dislikes
</span>
谢谢你。
2 个回答
3
为什么不使用YouTube 数据 API呢?视频源里面包含了一个
<gd:rating average='4.553648' max='5' min='1' numRaters='233' rel='http://schemas.google.com/g/2005#overall'/>
在每个<entry/>
里面。
2
我觉得现在的HTML看起来和你提供的那些不一样。下面是我在2017年2月时获取点赞数量的方法:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import urllib2
import html5lib
from bs4 import BeautifulSoup
url = "https://www.youtube.com/watch?v=DNMlW_5Bmv4"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page, 'html5lib')
soup.find("button",attrs={"title": "I like this"}).get_text()
# as of now, the number of upvote is 3240
# dislike is similar:
soup.find("button",attrs={"title": "I dislike this"}).get_text()
# which is 24 by now