擅长:python、mysql、java
<p>目前我只有一个解决方案,但它没有使用API,而且当有几千条评论时会变慢。</p>
<pre><code>import bs4, re, urllib2
#grab the page source for vide
data = urllib2.urlopen(r'http://www.youtube.com/all_comments?v=video_id') #example XhFtHW4YB7M
#pull out comments
soup = bs4.BeautifulSoup(data)
cmnts = soup.findAll(attrs={'class': 'comment yt-tile-default'})
#do something with them, ie count them
print len(cmnts)
</code></pre>
<p>请注意,由于“class”是内置的python名称,因此无法通过regex或lambdas对“startwith”进行常规搜索,如<a href="https://stackoverflow.com/a/2830550/541208">here</a>所示,因为您使用的是dict,而不是常规参数。由于BeautifulSoup,它也变得相当慢,但它需要使用,因为<code>etree</code>和<code>minidom</code>由于某种原因找不到匹配的标记。即使在<code>prettyfying()</code>和<code>bs4</code>之后</p>