Skip to main content
 首页 » 编程设计

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

2025年05月04日10sharpest

我有一个 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)。