旗下产业: A产业/ A实习/ A计划
全国统一咨询热线:010-5367 2995
首页 > 热门文章 > 大数据分析 > 大数据分析Python使用Last.fm API获取音乐数据

大数据分析Python使用Last.fm API获取音乐数据

时间:2020-05-28来源:www.aaa-cg.com.cn点击量:作者:Sissi
时间:2020-05-28点击量:作者:Sissi



  API使我们能够从服务器发出检索数据的请求。API在许多方面都很有用,但其中之一是能够为数据科学项目创建唯一的数据集。在本教程中,我们将学习一些用于Last.fm API的高级技术。
 

  在我们的初学者大数据分析Python API教程中,我们使用了一个简单的API,非常适合教授基础知识:
 

  a.它具有一些易于理解的终点。

  b.因为它不需要身份验证,所以我们不必担心如何告诉API我们有权使用它。

  c.每个端点响应的数据都很小,并且结构易于理解。
 

  实际上,大多数API都比这更复杂,因此要使用它们,您需要了解一些更高级的概念。具体来说,我们将学习:
 

  a.如何使用API​​密钥进行身份验证。

  b.如何使用速率限制和其他技术在API准则内工作。

  c.如何使用分页处理较大的响应。
 

  本教程假定您了解使用大数据分析Python使用API​​的基础知识。如果您不这样做,我们建议您开始我们的初学者API教程。我们还假设您具有大数据分析Python和pandas的中级知识。如果您不这样做,则可以通过我们的大数据分析Python基础课程免费开始学习。
 

  使用Last.fm API
 

  我们将使用Last.fm API。Last.fm是一项音乐服务,可通过连接到iTunes,Spotify等类似的音乐流应用程序并跟踪您听的音乐来建立个人资料。
 

  他们提供对API的免费访问权,以便音乐服务可以向其发送数据,还可以提供终结点,以汇总Last.fm在各种艺术家,歌曲和流派上拥有的所有数据。我们将使用他们的API建立热门艺术家的数据集。
 

  遵循API准则
 

  使用API​​时,请务必遵循其准则。如果您不这样做,则可能会被禁止使用该API。除此之外,特别是当一家公司免费提供API时,请尊重他们的限制和准则,因为他们没有提供任何东西。
 

  查看API文档中的Introduction页面,我们会注意到一些重要的准则:
 

  请在所有请求上使用可识别的User-Agent标头。这有助于我们进行日志记录,并减少被禁止的风险。
 

  向last.fm API发出请求时,可以使用headers标识自己。Last.fm希望我们在标头中指定一个用户代理,以便他们知道我们是谁。我们将在稍后提出第一个请求时学习如何执行此操作。
 

  在确定要拨打多少次电话时,请使用常识。例如,如果要制作Web应用程序,请尝试不要在页面加载时点击API。如果您的应用程序每秒持续拨打多个电话,则您的帐户可能会被暂停。
 

  为了构建我们的数据集,我们将需要向Last.fm API发出数千个请求。尽管他们没有在文档中提供具体的限制,但他们确实建议我们不要持续每秒进行多次呼叫。在本教程中,我们将学习一些限制速率的策略,或者确保我们不会过多使用它们的API,以便避免被禁止。
 

  在发出第一个请求之前,我们需要学习如何使用Last.fm API进行身份验证
 

  使用API​​密钥进行身份验证
 

  大多数API都要求您进行身份验证,以便他们知道您有权使用它们。身份验证的最常见形式之一是使用API密钥,就像使用其API的密码一样。如果在发出请求时未提供API密钥,则会出现错误。
 

  使用API​​密钥的过程如下:
 

  a.您使用API​​的提供者创建一个帐户。

  b.您需要一个API密钥,该密钥通常是一个长字符串,例如54686973206973206d7920415049204b6579。

  c.您可以将API密钥记录在安全的地方,例如密码保存器。如果有人获得了您的API密钥,那么他们可以使用伪装成您的API。

  d.每次发出请求时,您都提供API密钥以进行身份​​验证。
 

  要获取Last.fm的API密钥,请先创建一个帐户。创建帐户后,应转到以下表格:

大数据分析Python使用Last.fm API获取音乐数据
 

  在每个字段中填写有关您计划如何使用API​​的信息。您可以将“回调URL”字段保留为空白,因为仅当您正在构建要验证为特定Last.fm用户的Web应用程序时才使用此字段。
 

  提交表单后,您将获得API密钥和共享密钥的详细信息:

大数据分析Python使用Last.fm API获取音乐数据
 

  请在安全的地方记下这些内容-本教程无需使用共享密钥,但最好记下它,以防万一您想做一些需要您作为特定用户进行身份验证的事情。
 

  发出我们的第一个API请求
 

  为了创建热门艺术家的数据集,我们将使用chart.getTopArtists端点。
 

  查看Last.fm API文档,我们可以观察到以下几点:
 

  a.看起来只有一个真实的端点,并且实际上每个“端点”都是使用method参数指定的。

  b.文档说此服务不需要身份验证。尽管起初看起来似乎有些混乱,但它告诉我们的是,我们不需要身份验证为特定的Last.fm用户。如果您在此之上看,您会发现我们确实需要提供我们的API密钥。

  c.API可以返回多种格式的结果-我们将指定JSON,以便我们可以利用我们在大数据分析Python中使用API​​的已知知识
 

  在开始之前,请记住,当我们发出请求时,我们需要提供一个用户代理标头来标识自己。使用大数据分析Python请求库,我们使用headers参数和标头字典来指定标头,如下所示:
 

大数据分析Python使用Last.fm API获取音乐数据
 

  我们将从定义API密钥和用户代理开始(本教程中显示的API密钥不是真正的API密钥!)

大数据分析Python使用Last.fm API获取音乐数据
 

  接下来,我们将导入请求库,为标头和参数创建字典,然后发出第一个请求!

大数据分析Python使用Last.fm API获取音乐数据
 

  我们的请求返回的状态码为“ 200”,因此我们知道成功了。
 

  在查看请求返回的数据之前,请考虑一下在本教程中我们将发出许多请求的事实。在这些请求中,许多功能将是相同的:
 

  a.我们将使用相同的URL

  b.我们将使用相同的API密钥

  c.我们将指定JSON作为我们的格式。

  d.我们将使用相同的标题。
 

  为了节省时间,我们将创建一个函数来为我们完成很多工作。我们将为该函数提供一个有效负载字典,然后将额外的键添加到该字典,并将其与其他选项一起传递以发出请求。
 

  让我们看一下该函数的外观:

大数据分析Python使用Last.fm API获取音乐数据
 

  正如我们在初学者大数据分析Python API教程中所了解的那样,大多数API都以JSON格式返回数据,并且我们可以使用大数据分析Python json模块以更易于理解的格式打印JSON数据。
 

  让我们重新使用jprint()在该教程中创建的函数,并打印来自API的响应:

大数据分析Python使用Last.fm API获取音乐数据
 

  {

  "artists": {

  "@attr": {

  "page": "1",

  "perPage": "50",

  "total": "2901036",

  "totalPages": "58021"

  },

  "artist": [

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1957174",

  "mbid": "b7539c32-53e7-4908-bda3-81449c367da6",

  "name": "Lana Del Rey",

  "playcount": "232808939",

  "streamable": "0",

  "url": "https://www.last.fm/music/Lana+Del+Rey"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "588883",

  "mbid": "",

  "name": "Billie Eilish",

  "playcount": "35520548",

  "streamable": "0",

  "url": "https://www.last.fm/music/Billie+Eilish"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "655052",

  "mbid": "",

  "name": "Post Malone",

  "playcount": "34942708",

  "streamable": "0",

  "url": "https://www.last.fm/music/Post+Malone"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "2290993",

  "mbid": "20244d07-534f-4eff-b4d4-930878889970",

  "name": "Taylor Swift",

  "playcount": "196907702",

  "streamable": "0",

  "url": "https://www.last.fm/music/Taylor+Swift"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1166180",

  "mbid": "f4fdbb4c-e4b7-47a0-b83b-d91bbfcfa387",

  "name": "Ariana Grande",

  "playcount": "124251766",

  "streamable": "0",

  "url": "https://www.last.fm/music/Ariana+Grande"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1716587",

  "mbid": "b8a7c51f-362c-4dcb-a259-bc6e0095f0a6",

  "name": "Ed Sheeran",

  "playcount": "92646726",

  "streamable": "0",

  "url": "https://www.last.fm/music/Ed+Sheeran"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4084524",

  "mbid": "420ca290-76c5-41af-999e-564d7c71f1a7",

  "name": "Queen",

  "playcount": "197850122",

  "streamable": "0",

  "url": "https://www.last.fm/music/Queen"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4449534",

  "mbid": "164f0d73-1234-4e2c-8743-d77bf2191051",

  "name": "Kanye West",

  "playcount": "250533444",

  "streamable": "0",

  "url": "https://www.last.fm/music/Kanye+West"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3732023",

  "mbid": "b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d",

  "name": "The Beatles",

  "playcount": "526866107",

  "streamable": "0",

  "url": "https://www.last.fm/music/The+Beatles"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3441963",

  "mbid": "b49b81cc-d5b7-4bdd-aadb-385df8de69a6",

  "name": "Drake",

  "playcount": "147881092",

  "streamable": "0",

  "url": "https://www.last.fm/music/Drake"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4778856",

  "mbid": "a74b1b7f-71a5-4011-9441-d0b5e4122711",

  "name": "Radiohead",

  "playcount": "507682999",

  "streamable": "0",

  "url": "https://www.last.fm/music/Radiohead"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1528378",

  "mbid": "381086ea-f511-4aba-bdf9-71c753dc5077",

  "name": "Kendrick Lamar",

  "playcount": "109541321",

  "streamable": "0",

  "url": "https://www.last.fm/music/Kendrick+Lamar"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4618838",

  "mbid": "db36a76f-4cdf-43ac-8cd0-5e48092d2bae",

  "name": "Rihanna",

  "playcount": "204114198",

  "streamable": "0",

  "url": "https://www.last.fm/music/Rihanna"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3550620",

  "mbid": "ada7a83c-e3e1-40f1-93f9-3e73dbc9298a",

  "name": "Arctic Monkeys",

  "playcount": "339370140",

  "streamable": "0",

  "url": "https://www.last.fm/music/Arctic+Monkeys"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3401872",

  "mbid": "5441c29d-3602-4898-b1a1-b77fa23b8e50",

  "name": "David Bowie",

  "playcount": "197606611",

  "streamable": "0",

  "url": "https://www.last.fm/music/David+Bowie"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "680102",

  "mbid": "b7d92248-97e3-4450-8057-6fe06738f735",

  "name": "Shawn Mendes",

  "playcount": "28807659",

  "streamable": "0",

  "url": "https://www.last.fm/music/Shawn+Mendes"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "2093863",

  "mbid": "7e9bd05a-117f-4cce-87bc-e011527a8b18",

  "name": "Miley Cyrus",

  "playcount": "70227591",

  "streamable": "0",

  "url": "https://www.last.fm/music/Miley+Cyrus"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3631084",

  "mbid": "859d0860-d480-4efd-970c-c05d5f1776b8",

  "name": "Beyonc\u00e9",

  "playcount": "165422358",

  "streamable": "0",

  "url": "https://www.last.fm/music/Beyonc%C3%A9"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "314392",

  "mbid": "",

  "name": "Lil Nas X",

  "playcount": "6086745",

  "streamable": "0",

  "url": "https://www.last.fm/music/Lil+Nas+X"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3800669",

  "mbid": "122d63fc-8671-43e4-9752-34e846d62a9c",

  "name": "Katy Perry",

  "playcount": "151925781",

  "streamable": "0",

  "url": "https://www.last.fm/music/Katy+Perry"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1184440",

  "mbid": "63aa26c3-d59b-4da4-84ac-716b54f1ef4d",

  "name": "Tame Impala",

  "playcount": "78074910",

  "streamable": "0",

  "url": "https://www.last.fm/music/Tame+Impala"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3877144",

  "mbid": "650e7db6-b795-4eb5-a702-5ea2fc46c848",

  "name": "Lady Gaga",

  "playcount": "292105109",

  "streamable": "0",

  "url": "https://www.last.fm/music/Lady+Gaga"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "840762",

  "mbid": "f6beac20-5dfe-4d1f-ae02-0b0a740aafd6",

  "name": "Tyler, the Creator",

  "playcount": "56318705",

  "streamable": "0",

  "url": "https://www.last.fm/music/Tyler,+the+Creator"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "5429555",

  "mbid": "cc197bad-dc9c-440d-a5b5-d52ba2e14234",

  "name": "Coldplay",

  "playcount": "364107739",

  "streamable": "0",

  "url": "https://www.last.fm/music/Coldplay"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4665823",

  "mbid": "8bfac288-ccc5-448d-9573-c33ea2aa5c30",

  "name": "Red Hot Chili Peppers",

  "playcount": "298043491",

  "streamable": "0",

  "url": "https://www.last.fm/music/Red+Hot+Chili+Peppers"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "269116",

  "mbid": "50d06c43-5392-4393-ba2d-f091d8167227",

  "name": "Lizzo",

  "playcount": "6953283",

  "streamable": "0",

  "url": "https://www.last.fm/music/Lizzo"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "823024",

  "mbid": "260b6184-8828-48eb-945c-bc4cb6fc34ca",

  "name": "Charli XCX",

  "playcount": "34914186",

  "streamable": "0",

  "url": "https://www.last.fm/music/Charli+XCX"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "2246723",

  "mbid": "8dd98bdc-80ec-4e93-8509-2f46bafc09a7",

  "name": "Calvin Harris",

  "playcount": "75778154",

  "streamable": "0",

  "url": "https://www.last.fm/music/Calvin+Harris"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4320593",

  "mbid": "9282c8b4-ca0b-4c6b-b7e3-4f7762dfc4d6",

  "name": "Nirvana",

  "playcount": "226060468",

  "streamable": "0",

  "url": "https://www.last.fm/music/Nirvana"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1646166",

  "mbid": "bbfa1e12-8b61-4ed8-bc6e-52d2298f41a2",

  "name": "Tool",

  "playcount": "122824468",

  "streamable": "0",

  "url": "https://www.last.fm/music/Tool"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1008791",

  "mbid": "5a85c140-dcf9-4dd2-b2c8-aff0471549f3",

  "name": "Sam Smith",

  "playcount": "30910342",

  "streamable": "0",

  "url": "https://www.last.fm/music/Sam+Smith"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "2204118",

  "mbid": "bd13909f-1c29-4c27-a874-d4aaf27c5b1a",

  "name": "Fleetwood Mac",

  "playcount": "70574855",

  "streamable": "0",

  "url": "https://www.last.fm/music/Fleetwood+Mac"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3125893",

  "mbid": "83d91898-7763-47d7-b03b-b92132375c47",

  "name": "Pink Floyd",

  "playcount": "318740438",

  "streamable": "0",

  "url": "https://www.last.fm/music/Pink+Floyd"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "273449",

  "mbid": "",

  "name": "BROCKHAMPTON",

  "playcount": "36180740",

  "streamable": "0",

  "url": "https://www.last.fm/music/BROCKHAMPTON"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1326470",

  "mbid": "c8b03190-306c-4120-bb0b-6f2ebfc06ea9",

  "name": "The Weeknd",

  "playcount": "81295303",

  "streamable": "0",

  "url": "https://www.last.fm/music/The+Weeknd"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4569194",

  "mbid": "b95ce3ff-3d05-4e87-9e01-c97b66af13d4",

  "name": "Eminem",

  "playcount": "204081058",

  "streamable": "0",

  "url": "https://www.last.fm/music/Eminem"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3842009",

  "mbid": "b071f9fa-14b0-4217-8e97-eb41da73f598",

  "name": "The Rolling Stones",

  "playcount": "157067654",

  "streamable": "0",

  "url": "https://www.last.fm/music/The+Rolling+Stones"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1257764",

  "mbid": "e520459c-dff4-491d-a6e4-c97be35e0044",

  "name": "Frank Ocean",

  "playcount": "87152244",

  "streamable": "0",

  "url": "https://www.last.fm/music/Frank+Ocean"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3622199",

  "mbid": "e21857d5-3256-4547-afb3-4b6ded592596",

  "name": "Gorillaz",

  "playcount": "173334985",

  "streamable": "0",

  "url": "https://www.last.fm/music/Gorillaz"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "982956",

  "mbid": "7fb57fba-a6ef-44c2-abab-2fa3bdee607e",

  "name": "Childish Gambino",

  "playcount": "54111939",

  "streamable": "0",

  "url": "https://www.last.fm/music/Childish+Gambino"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3784477",

  "mbid": "084308bd-1654-436f-ba03-df6697104e19",

  "name": "Green Day",

  "playcount": "191486003",

  "streamable": "0",

  "url": "https://www.last.fm/music/Green+Day"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1710145",

  "mbid": "012151a8-0f9a-44c9-997f-ebd68b5389f9",

  "name": "Imagine Dragons",

  "playcount": "85440640",

  "streamable": "0",

  "url": "https://www.last.fm/music/Imagine+Dragons"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "1297869",

  "mbid": "8e494408-8620-4c6a-82c2-c2ca4a1e4f12",

  "name": "Lorde",

  "playcount": "76314107",

  "streamable": "0",

  "url": "https://www.last.fm/music/Lorde"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "997026",

  "mbid": "25b7b584-d952-4662-a8b9-dd8cdfbfeb64",

  "name": "A$AP Rocky",

  "playcount": "48732470",

  "streamable": "0",

  "url": "https://www.last.fm/music/A$AP+Rocky"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "2976178",

  "mbid": "69ee3720-a7cb-4402-b48d-a02c366f2bcf",

  "name": "The Cure",

  "playcount": "160080692",

  "streamable": "0",

  "url": "https://www.last.fm/music/The+Cure"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4472323",

  "mbid": "95e1ead9-4d31-4808-a7ac-32c3614c116b",

  "name": "The Killers",

  "playcount": "211585596",

  "streamable": "0",

  "url": "https://www.last.fm/music/The+Killers"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "2974470",

  "mbid": "d6e0e274-8e19-44ce-b5b2-52c12f8a674a",

  "name": "Led Zeppelin",

  "playcount": "191578475",

  "streamable": "0",

  "url": "https://www.last.fm/music/Led+Zeppelin"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "3825396",

  "mbid": "056e4f3e-d505-4dad-8ec1-d04f521cbb56",

  "name": "Daft Punk",

  "playcount": "212334330",

  "streamable": "0",

  "url": "https://www.last.fm/music/Daft+Punk"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "4019451",

  "mbid": "f59c5520-5f46-4d2c-b2c4-822eabf53419",

  "name": "Linkin Park",

  "playcount": "299550825",

  "streamable": "0",

  "url": "https://www.last.fm/music/Linkin+Park"

  },

  {

  "image": [

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "small"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "medium"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "large"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "extralarge"

  },

  {

  "#text": "https://lastfm-img2.akamaized.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png",

  "size": "mega"

  }

  ],

  "listeners": "487567",

  "mbid": "c6bfb05d-f570-46c8-98e1-e25441189770",

  "name": "Khalid",

  "playcount": "15759258",

  "streamable": "0",

  "url": "https://www.last.fm/music/Khalid"

  }

  ]

  }

  }
 

  JSON响应的结构为:
 

  1)具有单个artists键的字典,其中包含:

  2)@attr包含许多有关响应的属性 的键。

  3)artist包含艺术家对象列表 的键。
 

  让我们单独看一下'@attr'(属性)键:

大数据分析Python使用Last.fm API获取音乐数据


 

  这个API端点的结果中总共有近三百万位艺术家,我们正在单个页面中显示前50位艺术家。将结果分布在多个页面上的技术称为分页。
 

  处理分页数据
 

  为了建立包含许多艺术家的数据集,我们需要为每个页面提出一个API请求,然后将它们放在一起。我们可以使用文档中指定的两个可选参数来控制结果的分页:
 

  1)limit:每页要提取的结果数(默认为50)。

  2)page:我们要获取结果的哪一页。
 

  因为'@attrs'键为我们提供了总页数,所以我们可以使用while循环并在页面上进行迭代,直到页面号等于最后一个页面号为止。
 

  我们还可以使用limit参数在每个页面中获取更多结果-我们将在每个页面中获取500个结果,因此我们只需要进行约6,000次调用,而无需进行约60,000次调用。
 

  让我们看一下如何构造代码的示例:

大数据分析Python使用Last.fm API获取音乐数据
 

  正如我们刚才提到的,我们需要为此端点进行近6,000次调用,这意味着我们需要考虑速率限制以符合Last.fm API的服务条款。让我们看一些方法。
 

  限速
 

  速率限制是使用代码来限制我们达到特定API的每秒次数。速率限制将使您的代码变慢,但是比完全禁止使用API​​更好。
 

  执行速率限制的最简单方法是使用大数据分析Python time.sleep()函数。此函数接受一个浮点数,该浮点数指定要等待的秒数,然后再继续操作。
 

  例如,以下代码将在两个打印语句之间等待四分之一秒:

大数据分析Python使用Last.fm API获取音乐数据
 

  因为进行API调用本身需要一些时间,所以我们很可能每秒要进行2到3次调用,而不是每秒休眠0.25秒所建议的4次调用。这足以使我们保持在Last.fm的阈值以下(如果我们要打他们的API数小时,我们可能会选择一个更低的速率)。
 

  另一种对速率限制有用的技术是使用本地数据库来缓存任何API调用的结果,因此,如果我们两次进行相同的调用,则第二次从本地缓存中读取它。想象一下,在编写代码时,您发现语法错误并且循环失败,因此您必须重新开始。通过使用本地缓存,您有两个好处:
 

  a.您无需进行不必要的额外API调用。

  b.从缓存中读取重复的调用时,无需等待额外的时间进行速率限制。
 

  我们可以用来结合等待和缓存的逻辑如下所示:

大数据分析Python使用Last.fm API获取音乐数据
 

  为本地缓存创建逻辑是一项相当复杂的任务,但是有一个很棒的库称为requests-cache,该库只需几行代码即可为您完成所有工作。
 

  您可以使用pip安装请求缓存:

大数据分析Python使用Last.fm API获取音乐数据
 

  然后,我们要做的就是导入库并调用requests_cache.install_cache()函数,该库将透明地缓存新的API请求,并在每次重复调用时都使用该缓存。

大数据分析Python使用Last.fm API获取音乐数据
 

  我们应该考虑的最后一件事是,我们的6,000个请求可能会花费大约30分钟的时间,因此,我们将在每个循环中打印一些输出,以便可以看到所有内容。我们将使用I大数据分析Python显示技巧在每次运行后清除输出,以便使笔记本中的内容看起来更整洁。
 

  让我们开始吧!
 

  import time

  from I大数据分析Python.core.display import clear_output

  responses = []

  page = 1

  total_pages = 99999 # this is just a dummy number so the loop starts

  while page < = total_pages:

  payload = {

  'method': 'chart.gettopartists',

  'limit': 500,

  'page': page

  }

  # print some output so we can see the status

  print("Requesting page {}/{}".format(page, total_pages))

  # clear the output to make things neater

  clear_output(wait = True)

  # make the API call

  response = lastfm_get(payload)

  # if we get an error, print the response and halt the loop

  if response.status_code != 200:

  print(response.text)

  break

  # extract pagination info

  page = int(response.json()['artists']['@attr']['page'])

  total_pages = int(response.json()['artists']['@attr']['totalPages'])

  # append response

  responses.append(response)

  # if it's not a cached result, sleep

  if not getattr(response, 'from_cache', False):

  time.sleep(0.25)

  # increment the page number

  page += 1
 

  Requesting page 5803/5803
 

  第一次运行该代码,大约需要半个小时。由于存在缓存,第二次会更快(可能不到一分钟!)
 

  处理数据
 

  让我们使用熊猫来查看responses列表中第一个响应的数据:

大数据分析Python使用Last.fm API获取音乐数据
 

  我们可以使用列表推导对的每个响应执行此操作responses,为我们提供数据帧列表,然后使用该pandas.concat()函数将数据帧列表转换为单个数据帧。

大数据分析Python使用Last.fm API获取音乐数据
 

  我们的下一步将是删除图像列,其中包含艺术家图像的URL,从分析的角度来看,这些URL对我们并没有真正帮助。

大数据分析Python使用Last.fm API获取音乐数据
 

  现在,让我们使用DataFrame.info()和了解一些数据DataFrame.describe():

大数据分析Python使用Last.fm API获取音乐数据
 

  我们原本希望有3,000,000名艺术家,但我们只有10,500名。其中只有10,000个是唯一的(例如,有重复项)。
 

  让我们在响应对象列表中查看艺术家列表的长度,以了解是否可以更好地了解出了什么问题。

大数据分析Python使用Last.fm API获取音乐数据
 

  看起来我们的请求中只有20个具有响应列表-让我们按顺序查看前50个,看看是否有某种模式。

大数据分析Python使用Last.fm API获取音乐数据
 

  看起来在前20个响应之后,此API不会返回任何数据-未记录的限制。
 

  这不是世界末日,因为10,000名艺术家仍然是大量数据。让我们摆脱前面检测到的重复项。

大数据分析Python使用Last.fm API获取音乐数据
 

  使用第二个Last.fm API端点增强数据
 

  为了使我们的数据更有趣,让我们使用另一个last.fm API端点来添加有关每个艺术家的一些额外数据。
 

  Last.fm允许其用户创建“标签”以对艺术家进行分类。通过使用artist.getTopTags端点,我们可以从单个艺术家那里获得顶级标签。
 

  让我们以我们的一位艺术家为例,查看该端点的响应:

大数据分析Python使用Last.fm API获取音乐数据
 

  {

  "toptags": {

  "@attr": {

  "artist": "Lana Del Rey"

  },

  "tag": [

  {

  "count": 100,

  "name": "female vocalists",

  "url": "https://www.last.fm/tag/female+vocalists"

  },

  {

  "count": 93,

  "name": "indie",

  "url": "https://www.last.fm/tag/indie"

  },

  {

  "count": 88,

  "name": "indie pop",

  "url": "https://www.last.fm/tag/indie+pop"

  },

  {

  "count": 80,

  "name": "pop",

  "url": "https://www.last.fm/tag/pop"

  },

  {

  "count": 67,

  "name": "alternative",

  "url": "https://www.last.fm/tag/alternative"

  },

  {

  "count": 14,

  "name": "american",

  "url": "https://www.last.fm/tag/american"

  },

  {

  "count": 13,

  "name": "dream pop",

  "url": "https://www.last.fm/tag/dream+pop"

  },

  {

  "count": 12,

  "name": "seen live",

  "url": "https://www.last.fm/tag/seen+live"

  },

  {

  "count": 10,

  "name": "singer-songwriter",

  "url": "https://www.last.fm/tag/singer-songwriter"

  },

  {

  "count": 10,

  "name": "trip-hop",

  "url": "https://www.last.fm/tag/trip-hop"

  },

  {

  "count": 7,

  "name": "cult",

  "url": "https://www.last.fm/tag/cult"

  },

  {

  "count": 7,

  "name": "sadcore",

  "url": "https://www.last.fm/tag/sadcore"

  },

  {

  "count": 7,

  "name": "legend",

  "url": "https://www.last.fm/tag/legend"

  },

  {

  "count": 6,

  "name": "Lana Del Rey",

  "url": "https://www.last.fm/tag/Lana+Del+Rey"

  },

  {

  "count": 6,

  "name": "chamber pop",

  "url": "https://www.last.fm/tag/chamber+pop"

  },

  {

  "count": 5,

  "name": "baroque pop",

  "url": "https://www.last.fm/tag/baroque+pop"

  },

  {

  "count": 4,

  "name": "female vocalist",

  "url": "https://www.last.fm/tag/female+vocalist"

  },

  {

  "count": 4,

  "name": "alternative pop",

  "url": "https://www.last.fm/tag/alternative+pop"

  },

  {

  "count": 4,

  "name": "Retro",

  "url": "https://www.last.fm/tag/Retro"

  },

  {

  "count": 3,

  "name": "art pop",

  "url": "https://www.last.fm/tag/art+pop"

  },

  {

  "count": 3,

  "name": "chillout",

  "url": "https://www.last.fm/tag/chillout"

  },

  {

  "count": 3,

  "name": "soul",

  "url": "https://www.last.fm/tag/soul"

  },

  {

  "count": 3,

  "name": "USA",

  "url": "https://www.last.fm/tag/USA"

  },

  {

  "count": 3,

  "name": "sexy",

  "url": "https://www.last.fm/tag/sexy"

  },

  {

  "count": 2,

  "name": "new york",

  "url": "https://www.last.fm/tag/new+york"

  },

  {

  "count": 2,

  "name": "rock",

  "url": "https://www.last.fm/tag/rock"

  },

  {

  "count": 2,

  "name": "hollywood sadcore",

  "url": "https://www.last.fm/tag/hollywood+sadcore"

  },

  {

  "count": 2,

  "name": "indie rock",

  "url": "https://www.last.fm/tag/indie+rock"

  },

  {

  "count": 2,

  "name": "trip hop",

  "url": "https://www.last.fm/tag/trip+hop"

  },

  {

  "count": 2,

  "name": "female",

  "url": "https://www.last.fm/tag/female"

  },

  {

  "count": 2,

  "name": "jazz",

  "url": "https://www.last.fm/tag/jazz"

  },

  {

  "count": 2,

  "name": "10s",

  "url": "https://www.last.fm/tag/10s"

  },

  {

  "count": 2,

  "name": "folk",

  "url": "https://www.last.fm/tag/folk"

  },

  {

  "count": 2,

  "name": "lana",

  "url": "https://www.last.fm/tag/lana"

  },

  {

  "count": 2,

  "name": "electronic",

  "url": "https://www.last.fm/tag/electronic"

  },

  {

  "count": 2,

  "name": "blues",

  "url": "https://www.last.fm/tag/blues"

  },

  {

  "count": 2,

  "name": "2010s",

  "url": "https://www.last.fm/tag/2010s"

  },

  {

  "count": 2,

  "name": "vintage",

  "url": "https://www.last.fm/tag/vintage"

  },

  {

  "count": 2,

  "name": "alternative rock",

  "url": "https://www.last.fm/tag/alternative+rock"

  },

  {

  "count": 2,

  "name": "overrated",

  "url": "https://www.last.fm/tag/overrated"

  },

  {

  "count": 2,

  "name": "retro pop",

  "url": "https://www.last.fm/tag/retro+pop"

  },

  {

  "count": 2,

  "name": "beautiful",

  "url": "https://www.last.fm/tag/beautiful"

  },

  {

  "count": 2,

  "name": "melancholic",

  "url": "https://www.last.fm/tag/melancholic"

  },

  {

  "count": 2,

  "name": "dark pop",

  "url": "https://www.last.fm/tag/dark+pop"

  },

  {

  "count": 1,

  "name": "Hollywood Pop",

  "url": "https://www.last.fm/tag/Hollywood+Pop"

  },

  {

  "count": 1,

  "name": "chill",

  "url": "https://www.last.fm/tag/chill"

  },

  {

  "count": 1,

  "name": "americana",

  "url": "https://www.last.fm/tag/americana"

  },

  {

  "count": 1,

  "name": "diva",

  "url": "https://www.last.fm/tag/diva"

  },

  {

  "count": 1,

  "name": "sad",

  "url": "https://www.last.fm/tag/sad"

  }

  ]

  }

  }
 

  我们实际上只对标签名称感兴趣,然后仅对最受欢迎的标签感兴趣。让我们使用列表推导来创建前三个标记名称的列表:

大数据分析Python使用Last.fm API获取音乐数据
 

  然后我们可以使用该str.join()方法将列表转换为字符串:

 

大数据分析Python使用Last.fm API获取音乐数据


  让我们创建一个函数,使用该逻辑为所有艺术家返回最流行标签的字符串,我们稍后将使用该字符串应用于数据框中的每一行。
 

  请记住,该函数将大量连续使用,因此我们将重用time.sleep()先前的逻辑。


大数据分析Python使用Last.fm API获取音乐数据

 

  将此功能应用于我们的10,000行将需要不到一个小时的时间。因此,我们知道事情实际上正在进展中,我们将希望像以前一样监视输出的操作。
 

  不幸的是,在将函数与pandas Series.apply()方法一起应用时,我们不能使用手动打印输出的方法。相反,我们将使用tqdm软件包来自动执行此操作。

大数据分析Python使用Last.fm API获取音乐数据
 

  让我们看一下操作的结果:

大数据分析Python使用Last.fm API获取音乐数据
 

  完成并导出数据
 

  在导出数据之前,我们可能希望对数据进行排序,以便将最受欢迎的艺术家放在顶部。到目前为止,我们只是将数据存储为文本而不转换任何类型:

大数据分析Python使用Last.fm API获取音乐数据
 

  首先,将侦听器和播放计数列转换为数字。
 

  artists[["playcount", "listeners"]] = artists[["playcount", "listeners"]].astype(int)
 

  现在,让我们按听众数量排序

大数据分析Python使用Last.fm API获取音乐数据
 

  最后,我们可以将数据集导出为CSV文件。

大数据分析Python使用Last.fm API获取音乐数据
 

  下一步
 

  在大数据分析Python使用Last.fm API获取音乐数据中,我们学习了如何使用大数据分析Python来使用Last.fm API构建数据集:
 

  a.如何使用API​​密钥通过API进行身份验证

  b.如何使用分页从API端点收集更大的响应

  c.如何使用速率限制以保持在API准则之内。
 

  如果您想扩展学习范围,则可以:
 

  a.完成我们的交互式Dataquest API和抓取过程,您可以免费开始。

  b.探索Lasts.fm API中的其他API端点。

  c.尝试使用此免费公共API列表中的一些数据。


 

预约申请免费试听课

填写下面表单即可预约申请免费试听!怕钱不够?可先就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可推荐就业!

©2007-2021/北京漫动者教育科技有限公司版权所有
备案号:京ICP备12034770号

©2007-2022/ www.aaa-cg.com.cn 北京漫动者数字科技有限公司 备案号: 京ICP备12034770号 监督电话:010-53672995 邮箱:bjaaa@aaaedu.cc

京公网安备 11010802035704号

网站地图