gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:35 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com