Python:从 url 获取shoutcast/互联网广播电台的名称 [英] Python: Get name of shoutcast/internet radio station from url
问题描述
我一直在尝试根据 python 中的 url 获取网络广播电台的名称/标题,但到目前为止还没有成功.网络电台似乎使用了不同于 HTTP 的其他协议,但如果我错了,请纠正我.
有标题:伊维萨全球广播电台"
如何将此标题存储在变量中?任何帮助将不胜感激:)
亲切的问候,弗里格
从一点curl
来看,似乎是在使用shoutcast 协议,因此您正在寻找以 icy-name:
$ curl http://89.238.146.142:7030 |头-5% Total % Received % Xferd Average Speed Time Time Time Current下载上传总花费的剩余速度100 13191 0 13191 0 0 16013 0 --:--:-- --:--:-- --:--:-- 28516ICY 200 OKicy-notice1:<BR>此流需要 <a href="http://www.winamp.com/">Winamp</a><BR>icy-notice2:SHOUTcast 分布式网络音频服务器/Linux v1.9.8
冰雪名称:伊维萨全球广播电台冰冷类型:电子100 33463 0 33463 0 0 30954 0 --:--:-- 0:00:01 --:--:-- 46579curl: (23) 写入正文失败$
因此:
<预><代码>>>>导入 urllib2>>>f = urllib2.urlopen('http://89.238.146.142:7030')>>>对于 i,enumerate(f) 中的行:...如果 line.startswith('icy-name') 或 i >20:休息...>>>如果我>20:打印找不到站名"... else: 打印 '站名是', line.replace('icy-name:', '')...电台名称是 Ibiza Global Radio>>>您可能想添加例如一些 .lower()
调用,因为我相信这些标头名称是不区分大小写的,但这是总体思路.
I've been trying to get the name/title of internet radio stations based on the url in python, but with no luck so far. It seems that internet radio stations use another protocol than HTTP, but please correct me if I'm wrong.
For example: http://89.238.146.142:7030
Has the title: "Ibiza Global Radio"
How can i store this title in a variable? Any help will be deeply appreciated :)
Kind regards, frigg
From a little curl
, it seems to be using shoutcast protocol, so you're looking for an early line starting with 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
$
Therefore:
>>> 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
>>>
You may want to add e.g. some .lower()
calls because I believe these header names are meant to be case-insensitive, but that's the general idea.
这篇关于Python:从 url 获取shoutcast/互联网广播电台的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!