python - 简单抓取 youtube xml 以获得 Python 视频列表

标签 python xml youtube

我有一个 xml 提要,比如:

http://gdata.youtube.com/feeds/api/videos/-/bass/fishing/

我想获取视频的 href 列表:

 ['http://www.youtube.com/watch?v=aJvVkBcbFFY', 'ht....', ... ]

最佳答案

from xml.etree import cElementTree as ET
import urllib

def get_bass_fishing_URLs():
  results = []
  data = urllib.urlopen(
      'http://gdata.youtube.com/feeds/api/videos/-/bass/fishing/')
  tree = ET.parse(data)
  ns = '{http://www.w3.org/2005/Atom}'
  for entry in tree.findall(ns + 'entry'):
    for link in entry.findall(ns + 'link'):
      if link.get('rel') == 'alternate':
        results.append(link.get('href'))

您得到的似乎是所谓的“备用”链接。如果您想要稍微不同的东西,那么许多小的、可能的变化应该从上面的代码中清楚(加上 ElementTree 的标准 Python 库 docs)。

关于python - 简单抓取 youtube xml 以获得 Python 视频列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452144/

相关文章:

python - __mro__ 与其他双下划线名称有何不同?

python - Pycharm 2017.1 EAP Python 控制台和远程 Docker 解释器不能一起工作

python - 如何在Python DataFrame中为按行过滤的单元格设置值?

python - 插入不是默认 DJANGO 的数据库

java - XMLUnit : How to register a self-closing element as different to a non-self-closing element?

android - youtubeplayer在启动新视频时立即停止

ruby-on-rails - 减价中仅允许YouTube/Vimeo iframe

c# - 使用 C# 和 WPF 更新 XML 文件

android - xml中 fragment 中ListView上方的 float 操作按钮(FAB)?

javascript - 为什么 Youtube Data Api V3 中的 'gapi.client.youtube' 未定义?