dourun2990
dourun2990
采纳率100%
2011-10-05 21:10

从JSON文件中提取/抓取特定数据

这困扰了我好几个小时。 我一直在搜索很多东西,并且发现了很多信息。 问题是,我不是很好,实际上我是一名初学者。 我想用Python实现这一目标(如果可能的话!)。 也许还可以使用JavaScript和PHP? 我来解释一下。 我刚刚找到了这个网站http://listeningroom.net,它很棒。 你可以创建/加入房间并上传曲目并与朋友一起听。 我想从.json文件中提取/抓取/获取一些特定数据。 该文件包含艺术家、专辑标题、曲目标题等。 我只想提取艺术家、专辑和曲目标题。 http://listeningroom.net/room/chillasfuck/spins.json .json文件包含过去24小时内播放的曲目。 环顾四周后,我设法用Python抓取了整个.json文件(本地.json文件),其中包含以下可能不太有效的代码。
   json_data=open('...\spins.json')

   data = json.load(json_data)
   pprint(data)

   json_data.close()

显示出以下内容:

   [{u'endTime': u'1317752614105',
   u'id': u'cf37894e8eaf886a0d000000',
   u'length': 492330,
   u'metadata': {u'album': u'Mezzanine',
            u'artist': u'Massive Attack',
            u'bitrate': 128000,
            u'label': u'Virgin',
            u'length': 17494.479054779807,
            u'title': u'Group Four'},

这个只是显示的一部分内容

1、我想从一个网址(顶部提供的网址)中抓取它。2、仅获取“专辑”,“艺术家”和“标题” 3、确保它尽可能显示简单:
Artist
Track title
Album

Artist
Track title
Album

4、希望我能得到一些帮助,我真的很想为自己创建一个,这样我可以检查更多的音乐! 来自马文!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douyang5943 douyang5943 10年前

    Python (after you loaded the json)

    for elem in data:
        print('{artist}
    {title}
    {album}
    '.format(**elem['metadata']))
    

    To save in a file:

    with open('the_file_name.txt','w') as f:
        for elem in data:
            f.write('{artist}
    {title}
    {album}
    
    '.format(**elem['metadata']))
    
    点赞 评论 复制链接分享
  • dro44817 dro44817 10年前

    You're already really close.

    data = json.load(json_data)

    is taking the JSON string and converting it to a Python object - in this case, a list of dictionaries (plus 'metadata', which is a dictionary of dictionaries).

    To get this into the format that you want, you just need to loop through the items.

    for song in data:
        artist = song['metadata']['artist'] # This tells it where to look in the dictionary. It's looking for the dictionary item called 'metadata'. Then, looking inside that dictionary for 'artist'.
        album = song['metadata'['album']
        songTitle = song['metadata']['title']
        print '%s
    %s
    %s
    ' % (artist, album, songTitle)
    

    Or, to print it to a file:

    with open('the_file_name.txt','w') as f:
        for song in data:
            artist = song['metadata']['artist']
            album = song['metadata'['album']
            songTitle = song['metadata']['title']
            f.write('%s
    %s
    %s
    ' % (artist, album, songTitle))
    
    点赞 评论 复制链接分享
  • duan7772 duan7772 10年前

    For PHP you need json.decode

    <?php
          $json = file_get_contents($url);
          $val = json_decode($json);
          $room = $val[0]->metadata;
    echo "Album : ".$room->album."
    ";
    echo "Artist : ".$room->artist."
    ";
    echo "Title : ".$room->title."
    ";
    ?>
    

    Outputs

    Album  :  Future Sandwich
    Artist :  Them, Roaringtwenties
    Title  :  Fast Acting Nite-Nite Spray With Realistic Uncle Beard
    

    Note its a truck load of JSON data there so you'll have to iterate adequately

    点赞 评论 复制链接分享
  • douhang8991 douhang8991 10年前

    Okay this is a bit short but the thing about json is that it translate an array into a string

    eg. array['first'] = 'hello'; array['second'] = 'there';

    will become [{u'first': u'hello', u'second': 'there'}]; after a jsonencode run that sting throu jsondecode and you get your array back

    so simply run you json file thou a decoder and then you should be able to reach your data through:

    array['metadata'].album
    array['metadata'].artist
    ...
    

    have never used python but it should be the same.

    have a look at http://www.php.net/manual/en/function.json-decode.php it might clear upp a thing or two.

    点赞 评论 复制链接分享

相关推荐