如何使用python抓取等号后的值?

2024-05-29 09:42:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为link的字符串变量,它从远程站点返回数据。 如何解析等号(token=)后的数据? 例如,我想从 跟线。在

file: "http://www.aaastreams.com/playlist.m3u8?token=234132421reafdfasdfsdfdsf3234423edfasfdsf" 
});

python代码:

^{pr2}$

“print link;”中的示例响应值:

.......rest of response
    <script>

    jwplayer("container").setup({
    width:700,
    height:220,
    primary: "hls",
    title:"streams",
    autostart:true,

    image: "./1.jpg",
    file: "http://www.aaastreams.com/playlist.m3u8?token=234132421reafdfasdfsdfdsf3234423edfasfdsf" 
    });

    jwplayer().onError(function(){
    jwplayer().load({file:"http://www.aaa.com/jwplayer/ads.mp4",image:"http://aaa.com/2.png"});
    jwplayer().play();
    });

    </script>
.......rest of response

Tags: of数据comtokenresthttpresponsewww
3条回答

与其进一步解析url并假设整个字符串中只有一个等号,我建议您进行如下的字符串操作:

In [1]: s = "http://www.aaastreams.com/playlist.m3u8?token=234132421reafdfasdfsdfdsf3234423edfasfdsf"
In [2]: s.split('=')[1]
Out[3]: '234132421reafdfasdfsdfdsf3234423edfasfdsf'

解析URL的更好方法是使用urlparse模块。在

下面是一个例子:

from urlparse import urlparse, parse_qs

url = "http://www.aaastreams.com/playlist.m3u8?token=234132421reafdfasdfsdfdsf3234423edfasfdsf"
query = urlparse(url).query
params = parse_qs(query)

params将保存一个字典和您的令牌,以及url中的任何其他查询参数。在

考虑内置字符串方法:

str.partition(sep)

在第一次出现sep时拆分字符串,并返回一个3元组,该元组包含分隔符之前的部分、分隔符本身以及分隔符之后的部分。如果找不到分隔符,则返回一个包含字符串本身的3元组,后跟两个空字符串。在

在您的例子中,您可以使用“=”作为分隔符(sep)。str是包含“=”的长字符串。在

相关问题 更多 >

    热门问题