Python:如果URL文件中存在特定字符串,则打印一行

时间:2017-03-27 10:34:03

标签: python url hls

我正在用Python编写一个脚本,它应该打印到子配置文件的链接(01.m3u8,02.m3u8,108.m3u8等)。这是我的代码:

#!/usr/bin/env python

from urllib import quote
import requests

input_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

read_file = requests.get(input_file, verify = False)
for line in read_file:
        if "m3u8" in line:
                print line

输入流是HLS清单文件。这是它的语法:

#EXTM3U
#EXT-X-VERSION:4
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="deu_1",DEFAULT=YES,AUTOSELECT=YES,LANGUAGE="deu",URI="06.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="deu_2",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="deu",URI="07.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="DEU_32_149",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="DEU",URI="108.m3u8"
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=206701,RESOLUTION=320x180,FRAME-RATE=12.500,AUDIO="audio",SUBTITLES="subs"
01.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=681504,RESOLUTION=320x180,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
02.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1243460,RESOLUTION=480x270,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
03.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1917808,RESOLUTION=960x540,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
04.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3532769,RESOLUTION=1280x720,FRAME-RATE=50.000,AUDIO="audio",SUBTITLES="subs"
05.m3u8

当我运行我的脚本时,输出是:

LECT=YES,LANGUAGE="deu",URI="06.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="deu_2",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE=
"deu",URI="07.m3u8"
#EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="DEU_32_149",DEFAULT=NO,AUTOSELECT=YES,LANGUAGE="DEU",URI=
"108.m3u8"
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=206701,RESOLUTION=320x180,FRAME-RATE=12.500,AUDIO="audio",SUBTITLES="subs"

01.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=681504,RESOLUTION=320x180,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
0
2.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1243460,RESOLUTION=480x270,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
03
.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1917808,RESOLUTION=960x540,FRAME-RATE=25.000,AUDIO="audio",SUBTITLES="subs"
04.
m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3532769,RESOLUTION=1280x720,FRAME-RATE=50.000,AUDIO="audio",SUBTITLES="subs"
05.
m3u8

我知道目前我正在打印整条生产线,但为什么会报告:

05.
m3u8

而不是05.m3u8

1 个答案:

答案 0 :(得分:0)

显然Python中有一个m3u8库可以完全满足我的需求:

#!/usr/bin/env python
from urllib import quote
import m3u8

input_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

m3u8_main = m3u8.load(input_file)
for playlist in m3u8_main.playlists:
        print playlist.uri

完整文档可在以下网址找到:m3u8 library