0

我试图提取元素并将其分配给数组中的变量,但它给了我这个错误:

Traceback (most recent call last):
    File "test.py", line 12, in <module>
    itag_start =  element.find(itag)
AttributeError: 'Stream' object has no attribute 'find'

这是我的代码

from pytube import YouTube
link = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')
array = link.streams.all()
itag = 'itag'
mime_type='mime_type'
res='res'
counter = 0
iv = []
mt = []
re = []
    for element in array :
        itag_start =  element.find(itag)
        if itag_start != -1:
            itag_end = element.find('"', itag_start+len(itag)+2)
            itag_value = element[itag_start+len(itag)+1:itag_end+1]
            iv.insert(counter,itag_value)
        mime_type_start =  element.find(mime_type)
        if mime_type_start != -1:
            mime_type_end = element.find('"', mime_type_start+len(mime_type)+2)
            mime_type_value = element[mime_type_start+len(mime_type)+1:mime_type_end+1]
            mt.insert(counter,mime_type_value)

        res_start =  element.find(res)
        if res_start != -1:
            res_end = element.find('"', res_start+len(res)+2)
            res_value = element[res_start+len(res)+1:res_end+1]
            re.insert(counter,res_value)
         counter = counter + 1

但是,如果我直接用输出替换 link.streams.all() ,错误就会消失。

[<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">,
 <Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">,
 <Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">,
 <Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
 <Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">,
 <Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.640028">,
 <Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9">,
 <Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">,
 <Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9">,
 <Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e">,
 <Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">,
 <Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">,
 <Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9">,
 <Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">,
 <Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">,
 <Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">,
 <Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9">,
 <Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">,
 <Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">,
 <Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">,
 <Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">,
 <Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">]

关于如何解决这个问题的任何想法?

4

2 回答 2

0

更改您的线路:

textitag_start =  element.find(itag)

itag_start =  str(element).find(itag)

由您的语句array = link.streams.all() 生成的 PyTube 的流列表创建未引用字符串的列表元素。.find(itag)必须对字符串进行操作,这就是您收到错误的原因,PyTube 列表元素不是字符串。

使用str(element)将 PyTube 列表元素转换为字符串。请注意,.download语句中使用的 PyTube 列表元素不能用引号引起来,它们必须保留其在流列表中的原始格式。

于 2018-08-10T01:05:35.350 回答
0

如果你检查你得到的流的类型

video = yt.streams.all()
for i in video:
    print(type(i))

输出将是这样的:

<class 'pytube.streams.Stream'>

.find 属性仅适用于字符串类类型。使用 str 类型转换元素。

itag_start = str(element).find(itag)

并用 str(element).find 替换所有 element.find

于 2020-11-13T05:37:35.977 回答