<optgroup id="i0uik"></optgroup>
<optgroup id="i0uik"></optgroup>
旗下產業: A產業/ A實習/ A計劃
全國統一咨詢熱線:010-5367 2995
首頁 > 熱門文章 > 大數據分析 > 大數據分析Python使用Last.fm API獲取音樂數據
大數據分析Python使用Last.fm API獲取音樂數據
時間:2020-05-28來源:www.zhuozheng31.icu點擊量:作者: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-2019/北京漫動者教育科技有限公司版權所有
備案號:京ICP備12034770號

?2007-2019/ www.zhuozheng31.icu 北京漫動者教育科技有限公司 備案號:京ICP備12034770號 監督電話:010-62568622 郵箱:bjaaa@aaaedu.cc

久久久大香菇精品