代码之家  ›  专栏  ›  技术社区  ›  Fredrik

python:从url获取shoutcast/internet电台的名称

  •  2
  • Fredrik  · 技术社区  · 14 年前

    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.

    http://89.238.146.142:7030

    有头衔: “Ibiza全球电台”

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

    亲切的问候, 弗里格

    1 回复  |  直到 14 年前
        1
  •  7
  •   Alex Martelli    14 年前

    从一点 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
    $ 
    

    因此:

    >>> 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
    
    >>> 
    

    您可能需要添加,例如 .lower() calls because I believe these header names are meant to be case-insensitive, but that's the general idea.