在python上重复处理,但变量混合

时间:2018-11-03 20:36:23

标签: arrays python-3.x youtube-dl

目前,我正在尝试完成一个脚本,以将来自youtube播放列表的链接通过youtube_dl传递给我,以便能够将其中的音频流传输到我的树莓派上的播放器中,但是我不知道如何重复该过程,而是要进行更改自动更改变量,而无需更改数组上的每个数字,例如:

import os
import pafy
import youtube_dl as yt
from youtube_dl import YoutubeDL

links = ["http://www.youtube.com/watch?v=JaSfjAIcGpQ",
        "http://www.youtube.com/watch?v=pzAo3Hj15R4",
        "http://www.youtube.com/watch?v=hWdovALEen0",
        "http://www.youtube.com/watch?v=JVpTp8IHdEg",
        "http://www.youtube.com/watch?v=Pw-0pbY9JeU",
        "http://www.youtube.com/watch?v=lrfhf1Gv4Tw",
        "http://www.youtube.com/watch?v=ilw-qmqZ5zY",
        "http://www.youtube.com/watch?v=UwsY_foobEw",
        "http://www.youtube.com/watch?v=RSNmgE6L8AU",
        "http://www.youtube.com/watch?v=d020hcWA_Wg",
        "http://www.youtube.com/watch?v=O4irXQhgMqg",
        "http://www.youtube.com/watch?v=9P16xvwMQ5A",
        "http://www.youtube.com/watch?v=fregObNcHC8",
        "http://www.youtube.com/watch?v=fLN6ec7-43s",
        "http://www.youtube.com/watch?v=DcHKOC64KnE",
        "http://www.youtube.com/watch?v=UYwF-jdcVjY",
        "http://www.youtube.com/watch?v=jQcBwE6j09U",
        "http://www.youtube.com/watch?v=lXgkuM2NhYI",
        "http://www.youtube.com/watch?v=vabnZ9-ex7o",
        "http://www.youtube.com/watch?v=neNJvLIKaOk",
        "http://www.youtube.com/watch?v=k4V3Mo61fJM",
        "http://www.youtube.com/watch?v=pstVCGyaUBM",
        "http://www.youtube.com/watch?v=D8Ymd-OCucs",
        "http://www.youtube.com/watch?v=SeRJb3U1v3A",
        "http://www.youtube.com/watch?v=J0DjcsK_-HY",
        "http://www.youtube.com/watch?v=f2JuxM-snGc",
        "http://www.youtube.com/watch?v=Eyjj8BgsBGU",
        "http://www.youtube.com/watch?v=dX3k_QDnzHE",
        "http://www.youtube.com/watch?v=luM6oeCM7Yw",
        "https://www.youtube.com/watch?v=5iC0YXspJRM",
        "https://www.youtube.com/watch?v=BnOVufgbIzA",
        "https://www.youtube.com/watch?v=wwK_Kmh0P90",
        "https://www.youtube.com/watch?v=UYfh9YhUVdE"]


y = YoutubeDL({
    'format': 'bestaudio',
})
url = links[0]
r = y.extract_info(url, download=False)
print(r['ext'])
print(r['url'])

我正在尝试更改“链接[0]”的部分 所以我可以重复此过程,但无需手动更改数字,而是在python中进行。我还是python的新手。

2 个答案:

答案 0 :(得分:0)

据我了解,您想遍历列表。您可以使用for循环来实现它。
像这样:

for link in links:
    url = link
    r = y.extract_info(url, download=False)

答案 1 :(得分:0)

上面的答案似乎还可以,只是添加其他方式来做到这一点: 最短版本:

extracted_details_list = [y.extract_info(url, download=False) for url in links] # list comprehension

长版本,可能更容易获得for循环的流:

for i in range(len(links)):
    url = links[i]
    r = y.extract_info(url, download=False)
    print(r['ext'])
    print(r['url']