【发布时间】:2011-09-08 09:56:51
【问题描述】:
我正在尝试抓取 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
我似乎无法获得所需的其他信息。 reference guide 表示我可以从 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()
【问题讨论】: