Python:从url获取shoutcast/internet广播电台的名称

fri*_*igg 3 python shoutcast radio

我一直在尝试根据python中的url获取互联网广播电台的名称/标题,但到目前为止还没有运气.似乎互联网广播电台使用的是除HTTP之外的其他协议,但如果我错了,请纠正我.

例如:http://89.238.146.142: 7030

标题是:"Ibiza Global Radio"

如何将此标题存储在变量中?任何帮助将深深感激:)

亲切的问候,frigg

Ale*_*lli 8

从一点点来看curl,它似乎是使用shoutcast协议,所以你正在寻找一个早期的行开头icy-name:

$ curl http://89.238.146.142:7030 | head -5
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13191    0 13191    0     0  16013      0 --:--:-- --:--:-- --:--:-- 28516ICY 200 OK
icy-notice1:<BR>This stream requires <a href="http://www.winamp.com/">Winamp</a><BR>
icy-notice2:SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
icy-name:Ibiza Global Radio
icy-genre:Electronic
100 33463    0 33463    0     0  30954      0 --:--:--  0:00:01 --:--:-- 46579
curl: (23) Failed writing body
$ 
Run Code Online (Sandbox Code Playgroud)

因此:

>>> import urllib2
>>> f = urllib2.urlopen('http://89.238.146.142:7030')
>>> for i, line in enumerate(f):
...   if line.startswith('icy-name') or i > 20: break
... 
>>> if i > 20: print 'failed to find station name'
... else: print 'station name is', line.replace('icy-name:', '')
... 
station name is Ibiza Global Radio

>>> 
Run Code Online (Sandbox Code Playgroud)

您可能想要添加例如一些.lower()调用,因为我相信这些标题名称是不区分大小写的,但这是一般的想法.