抓取YouTube用户信息
我正在尝试抓取YouTube的信息,想要获取大约200个用户的相关信息。我对这些用户之间的关系很感兴趣,比如:
- 联系人
- 订阅者
- 他们订阅了哪些频道
- 他们评论了哪些视频
- 等等
我已经通过以下代码获取到了联系信息:
import gdata.youtube
import gdata.youtube.service
from gdata.service import RequestError
from pub_author import KEY, NAME_REGEX
def get_details(name):
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = KEY
contact_feed = yt_service.GetYouTubeContactFeed(username=name)
contacts = [ e.title.text for e in contact_feed.entry ]
return contacts
但是我似乎无法获取到其他我需要的信息。参考指南说我可以从http://gdata.youtube.com/feeds/api/users/username/subscriptions?v=2(对于某个用户)抓取XML数据。不过,如果我尝试获取其他用户的订阅信息,就会收到403错误,提示信息是:
用户必须登录才能访问这些订阅。
如果我使用gdata API:
sub_feed = yt_service.GetYouTubeSubscriptionFeed(username=name)
sub = [ e.title.text for e in contact_feed.entry ]
我也会得到同样的错误。
我该如何在不登录的情况下获取这些订阅信息呢?这应该是可以的,因为你可以在不登录YouTube网站的情况下访问这些信息。
另外,似乎没有特定用户的订阅者信息的接口。这些信息通过API能获取到吗?
编辑
看起来通过API是无法做到这一点的。我不得不采取一种快速而粗暴的方法:
for f in `cat users.txt`; do wget "www.youtube.com/profile?user=$f&view=subscriptions" --output-document subscriptions/$f.html; done
然后使用这个脚本从下载的HTML文件中提取用户名:
"""Extract usernames from a Youtube profile using regex"""
import re
def main():
import sys
lines = open(sys.argv[1]).read().split('\n')
#
# The html files has two <a href="..."> tags for each user: once for an
# image thumbnail, and once for a text link.
#
users = set()
for l in lines:
match = re.search('<a href="/user/(?P<name>[^"]+)" onmousedown', l)
if match:
users.add(match.group('name'))
users = list(users)
users.sort()
print users
if __name__ == '__main__':
main()
1 个回答
2
为了在用户没有登录的情况下访问他们的订阅内容,用户需要在他们的账户共享设置中勾选“订阅频道”这个选项。
目前,通过gdata
API没有直接的方法可以获取一个频道的订阅者。实际上,这个功能的请求已经提出超过3年了,但仍然没有解决!你可以查看如何获取用户的订阅者列表?。