旗下產(chǎn)業(yè): A產(chǎn)業(yè)/?A實(shí)習(xí)/?A計(jì)劃
全國(guó)統(tǒng)一咨詢熱線:010-5367 2995
首頁(yè) > 熱門文章 > 大數(shù)據(jù)分析 > 大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)

時(shí)間:2020-05-28來(lái)源:5wd995.cn點(diǎn)擊量:作者:Sissi
時(shí)間:2020-05-28點(diǎn)擊量:作者:Sissi



  API使我們能夠從服務(wù)器發(fā)出檢索數(shù)據(jù)的請(qǐng)求。API在許多方面都很有用,但其中之一是能夠?yàn)閿?shù)據(jù)科學(xué)項(xiàng)目創(chuàng)建唯一的數(shù)據(jù)集。在本教程中,我們將學(xué)習(xí)一些用于Last.fm API的高級(jí)技術(shù)。
 

  在我們的初學(xué)者大數(shù)據(jù)分析Python API教程中,我們使用了一個(gè)簡(jiǎn)單的API,非常適合教授基礎(chǔ)知識(shí):
 

  a.它具有一些易于理解的終點(diǎn)。

  b.因?yàn)樗恍枰矸蒡?yàn)證,所以我們不必?fù)?dān)心如何告訴API我們有權(quán)使用它。

  c.每個(gè)端點(diǎn)響應(yīng)的數(shù)據(jù)都很小,并且結(jié)構(gòu)易于理解。
 

  實(shí)際上,大多數(shù)API都比這更復(fù)雜,因此要使用它們,您需要了解一些更高級(jí)的概念。具體來(lái)說(shuō),我們將學(xué)習(xí):
 

  a.如何使用API??密鑰進(jìn)行身份驗(yàn)證。

  b.如何使用速率限制和其他技術(shù)在API準(zhǔn)則內(nèi)工作。

  c.如何使用分頁(yè)處理較大的響應(yīng)。
 

  本教程假定您了解使用大數(shù)據(jù)分析Python使用API??的基礎(chǔ)知識(shí)。如果您不這樣做,我們建議您開(kāi)始我們的初學(xué)者API教程。我們還假設(shè)您具有大數(shù)據(jù)分析Python和pandas的中級(jí)知識(shí)。如果您不這樣做,則可以通過(guò)我們的大數(shù)據(jù)分析Python基礎(chǔ)課程免費(fèi)開(kāi)始學(xué)習(xí)。
 

  使用Last.fm API
 

  我們將使用Last.fm API。Last.fm是一項(xiàng)音樂(lè)服務(wù),可通過(guò)連接到iTunes,Spotify等類似的音樂(lè)流應(yīng)用程序并跟蹤您聽(tīng)的音樂(lè)來(lái)建立個(gè)人資料。
 

  他們提供對(duì)API的免費(fèi)訪問(wèn)權(quán),以便音樂(lè)服務(wù)可以向其發(fā)送數(shù)據(jù),還可以提供終結(jié)點(diǎn),以匯總Last.fm在各種藝術(shù)家,歌曲和流派上擁有的所有數(shù)據(jù)。我們將使用他們的API建立熱門藝術(shù)家的數(shù)據(jù)集。
 

  遵循API準(zhǔn)則
 

  使用API??時(shí),請(qǐng)務(wù)必遵循其準(zhǔn)則。如果您不這樣做,則可能會(huì)被禁止使用該API。除此之外,特別是當(dāng)一家公司免費(fèi)提供API時(shí),請(qǐng)尊重他們的限制和準(zhǔn)則,因?yàn)樗麄儧](méi)有提供任何東西。
 

  查看API文檔中的Introduction頁(yè)面,我們會(huì)注意到一些重要的準(zhǔn)則:
 

  請(qǐng)?jiān)谒姓?qǐng)求上使用可識(shí)別的User-Agent標(biāo)頭。這有助于我們進(jìn)行日志記錄,并減少被禁止的風(fēng)險(xiǎn)。
 

  向last.fm API發(fā)出請(qǐng)求時(shí),可以使用headers標(biāo)識(shí)自己。Last.fm希望我們?cè)跇?biāo)頭中指定一個(gè)用戶代理,以便他們知道我們是誰(shuí)。我們將在稍后提出第一個(gè)請(qǐng)求時(shí)學(xué)習(xí)如何執(zhí)行此操作。
 

  在確定要撥打多少次電話時(shí),請(qǐng)使用常識(shí)。例如,如果要制作Web應(yīng)用程序,請(qǐng)嘗試不要在頁(yè)面加載時(shí)點(diǎn)擊API。如果您的應(yīng)用程序每秒持續(xù)撥打多個(gè)電話,則您的帳戶可能會(huì)被暫停。
 

  為了構(gòu)建我們的數(shù)據(jù)集,我們將需要向Last.fm API發(fā)出數(shù)千個(gè)請(qǐng)求。盡管他們沒(méi)有在文檔中提供具體的限制,但他們確實(shí)建議我們不要持續(xù)每秒進(jìn)行多次呼叫。在本教程中,我們將學(xué)習(xí)一些限制速率的策略,或者確保我們不會(huì)過(guò)多使用它們的API,以便避免被禁止。
 

  在發(fā)出第一個(gè)請(qǐng)求之前,我們需要學(xué)習(xí)如何使用Last.fm API進(jìn)行身份驗(yàn)證
 

  使用API??密鑰進(jìn)行身份驗(yàn)證
 

  大多數(shù)API都要求您進(jìn)行身份驗(yàn)證,以便他們知道您有權(quán)使用它們。身份驗(yàn)證的最常見(jiàn)形式之一是使用API密鑰,就像使用其API的密碼一樣。如果在發(fā)出請(qǐng)求時(shí)未提供API密鑰,則會(huì)出現(xiàn)錯(cuò)誤。
 

  使用API??密鑰的過(guò)程如下:
 

  a.您使用API??的提供者創(chuàng)建一個(gè)帳戶。

  b.您需要一個(gè)API密鑰,該密鑰通常是一個(gè)長(zhǎng)字符串,例如54686973206973206d7920415049204b6579。

  c.您可以將API密鑰記錄在安全的地方,例如密碼保存器。如果有人獲得了您的API密鑰,那么他們可以使用偽裝成您的API。

  d.每次發(fā)出請(qǐng)求時(shí),您都提供API密鑰以進(jìn)行身份??驗(yàn)證。
 

  要獲取Last.fm的API密鑰,請(qǐng)先創(chuàng)建一個(gè)帳戶。創(chuàng)建帳戶后,應(yīng)轉(zhuǎn)到以下表格:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  在每個(gè)字段中填寫有關(guān)您計(jì)劃如何使用API??的信息。您可以將“回調(diào)URL”字段保留為空白,因?yàn)閮H當(dāng)您正在構(gòu)建要驗(yàn)證為特定Last.fm用戶的Web應(yīng)用程序時(shí)才使用此字段。
 

  提交表單后,您將獲得API密鑰和共享密鑰的詳細(xì)信息:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  請(qǐng)?jiān)诎踩牡胤接浵逻@些內(nèi)容-本教程無(wú)需使用共享密鑰,但最好記下它,以防萬(wàn)一您想做一些需要您作為特定用戶進(jìn)行身份驗(yàn)證的事情。
 

  發(fā)出我們的第一個(gè)API請(qǐng)求
 

  為了創(chuàng)建熱門藝術(shù)家的數(shù)據(jù)集,我們將使用chart.getTopArtists端點(diǎn)。
 

  查看Last.fm API文檔,我們可以觀察到以下幾點(diǎn):
 

  a.看起來(lái)只有一個(gè)真實(shí)的端點(diǎn),并且實(shí)際上每個(gè)“端點(diǎn)”都是使用method參數(shù)指定的。

  b.文檔說(shuō)此服務(wù)不需要身份驗(yàn)證。盡管起初看起來(lái)似乎有些混亂,但它告訴我們的是,我們不需要身份驗(yàn)證為特定的Last.fm用戶。如果您在此之上看,您會(huì)發(fā)現(xiàn)我們確實(shí)需要提供我們的API密鑰。

  c.API可以返回多種格式的結(jié)果-我們將指定JSON,以便我們可以利用我們?cè)诖髷?shù)據(jù)分析Python中使用API??的已知知識(shí)
 

  在開(kāi)始之前,請(qǐng)記住,當(dāng)我們發(fā)出請(qǐng)求時(shí),我們需要提供一個(gè)用戶代理標(biāo)頭來(lái)標(biāo)識(shí)自己。使用大數(shù)據(jù)分析Python請(qǐng)求庫(kù),我們使用headers參數(shù)和標(biāo)頭字典來(lái)指定標(biāo)頭,如下所示:
 

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  我們將從定義API密鑰和用戶代理開(kāi)始(本教程中顯示的API密鑰不是真正的API密鑰!)

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  接下來(lái),我們將導(dǎo)入請(qǐng)求庫(kù),為標(biāo)頭和參數(shù)創(chuàng)建字典,然后發(fā)出第一個(gè)請(qǐng)求!

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  我們的請(qǐng)求返回的狀態(tài)碼為“ 200”,因此我們知道成功了。
 

  在查看請(qǐng)求返回的數(shù)據(jù)之前,請(qǐng)考慮一下在本教程中我們將發(fā)出許多請(qǐng)求的事實(shí)。在這些請(qǐng)求中,許多功能將是相同的:
 

  a.我們將使用相同的URL

  b.我們將使用相同的API密鑰

  c.我們將指定JSON作為我們的格式。

  d.我們將使用相同的標(biāo)題。
 

  為了節(jié)省時(shí)間,我們將創(chuàng)建一個(gè)函數(shù)來(lái)為我們完成很多工作。我們將為該函數(shù)提供一個(gè)有效負(fù)載字典,然后將額外的鍵添加到該字典,并將其與其他選項(xiàng)一起傳遞以發(fā)出請(qǐng)求。
 

  讓我們看一下該函數(shù)的外觀:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  正如我們?cè)诔鯇W(xué)者大數(shù)據(jù)分析Python API教程中所了解的那樣,大多數(shù)API都以JSON格式返回?cái)?shù)據(jù),并且我們可以使用大數(shù)據(jù)分析Python json模塊以更易于理解的格式打印JSON數(shù)據(jù)。
 

  讓我們重新使用jprint()在該教程中創(chuàng)建的函數(shù),并打印來(lái)自API的響應(yīng):

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  {

  "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響應(yīng)的結(jié)構(gòu)為:
 

  1)具有單個(gè)artists鍵的字典,其中包含:

  2)@attr包含許多有關(guān)響應(yīng)的屬性 的鍵。

  3)artist包含藝術(shù)家對(duì)象列表 的鍵。
 

  讓我們單獨(dú)看一下'@attr'(屬性)鍵:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)


 

  這個(gè)API端點(diǎn)的結(jié)果中總共有近三百萬(wàn)位藝術(shù)家,我們正在單個(gè)頁(yè)面中顯示前50位藝術(shù)家。將結(jié)果分布在多個(gè)頁(yè)面上的技術(shù)稱為分頁(yè)。
 

  處理分頁(yè)數(shù)據(jù)
 

  為了建立包含許多藝術(shù)家的數(shù)據(jù)集,我們需要為每個(gè)頁(yè)面提出一個(gè)API請(qǐng)求,然后將它們放在一起。我們可以使用文檔中指定的兩個(gè)可選參數(shù)來(lái)控制結(jié)果的分頁(yè):
 

  1)limit:每頁(yè)要提取的結(jié)果數(shù)(默認(rèn)為50)。

  2)page:我們要獲取結(jié)果的哪一頁(yè)。
 

  因?yàn)?#39;@attrs'鍵為我們提供了總頁(yè)數(shù),所以我們可以使用while循環(huán)并在頁(yè)面上進(jìn)行迭代,直到頁(yè)面號(hào)等于最后一個(gè)頁(yè)面號(hào)為止。
 

  我們還可以使用limit參數(shù)在每個(gè)頁(yè)面中獲取更多結(jié)果-我們將在每個(gè)頁(yè)面中獲取500個(gè)結(jié)果,因此我們只需要進(jìn)行約6,000次調(diào)用,而無(wú)需進(jìn)行約60,000次調(diào)用。
 

  讓我們看一下如何構(gòu)造代碼的示例:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  正如我們剛才提到的,我們需要為此端點(diǎn)進(jìn)行近6,000次調(diào)用,這意味著我們需要考慮速率限制以符合Last.fm API的服務(wù)條款。讓我們看一些方法。
 

  限速
 

  速率限制是使用代碼來(lái)限制我們達(dá)到特定API的每秒次數(shù)。速率限制將使您的代碼變慢,但是比完全禁止使用API??更好。
 

  執(zhí)行速率限制的最簡(jiǎn)單方法是使用大數(shù)據(jù)分析Python time.sleep()函數(shù)。此函數(shù)接受一個(gè)浮點(diǎn)數(shù),該浮點(diǎn)數(shù)指定要等待的秒數(shù),然后再繼續(xù)操作。
 

  例如,以下代碼將在兩個(gè)打印語(yǔ)句之間等待四分之一秒:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  因?yàn)檫M(jìn)行API調(diào)用本身需要一些時(shí)間,所以我們很可能每秒要進(jìn)行2到3次調(diào)用,而不是每秒休眠0.25秒所建議的4次調(diào)用。這足以使我們保持在Last.fm的閾值以下(如果我們要打他們的API數(shù)小時(shí),我們可能會(huì)選擇一個(gè)更低的速率)。
 

  另一種對(duì)速率限制有用的技術(shù)是使用本地?cái)?shù)據(jù)庫(kù)來(lái)緩存任何API調(diào)用的結(jié)果,因此,如果我們兩次進(jìn)行相同的調(diào)用,則第二次從本地緩存中讀取它。想象一下,在編寫代碼時(shí),您發(fā)現(xiàn)語(yǔ)法錯(cuò)誤并且循環(huán)失敗,因此您必須重新開(kāi)始。通過(guò)使用本地緩存,您有兩個(gè)好處:
 

  a.您無(wú)需進(jìn)行不必要的額外API調(diào)用。

  b.從緩存中讀取重復(fù)的調(diào)用時(shí),無(wú)需等待額外的時(shí)間進(jìn)行速率限制。
 

  我們可以用來(lái)結(jié)合等待和緩存的邏輯如下所示:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  為本地緩存創(chuàng)建邏輯是一項(xiàng)相當(dāng)復(fù)雜的任務(wù),但是有一個(gè)很棒的庫(kù)稱為requests-cache,該庫(kù)只需幾行代碼即可為您完成所有工作。
 

  您可以使用pip安裝請(qǐng)求緩存:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  然后,我們要做的就是導(dǎo)入庫(kù)并調(diào)用requests_cache.install_cache()函數(shù),該庫(kù)將透明地緩存新的API請(qǐng)求,并在每次重復(fù)調(diào)用時(shí)都使用該緩存。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  我們應(yīng)該考慮的最后一件事是,我們的6,000個(gè)請(qǐng)求可能會(huì)花費(fèi)大約30分鐘的時(shí)間,因此,我們將在每個(gè)循環(huán)中打印一些輸出,以便可以看到所有內(nèi)容。我們將使用I大數(shù)據(jù)分析Python顯示技巧在每次運(yùn)行后清除輸出,以便使筆記本中的內(nèi)容看起來(lái)更整潔。
 

  讓我們開(kāi)始吧!
 

  import time

  from I大數(shù)據(jù)分析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
 

  第一次運(yùn)行該代碼,大約需要半個(gè)小時(shí)。由于存在緩存,第二次會(huì)更快(可能不到一分鐘!)
 

  處理數(shù)據(jù)
 

  讓我們使用熊貓來(lái)查看responses列表中第一個(gè)響應(yīng)的數(shù)據(jù):

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  我們可以使用列表推導(dǎo)對(duì)的每個(gè)響應(yīng)執(zhí)行此操作responses,為我們提供數(shù)據(jù)幀列表,然后使用該pandas.concat()函數(shù)將數(shù)據(jù)幀列表轉(zhuǎn)換為單個(gè)數(shù)據(jù)幀。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  我們的下一步將是刪除圖像列,其中包含藝術(shù)家圖像的URL,從分析的角度來(lái)看,這些URL對(duì)我們并沒(méi)有真正幫助。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  現(xiàn)在,讓我們使用DataFrame.info()和了解一些數(shù)據(jù)DataFrame.describe():

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  我們?cè)鞠M?,000,000名藝術(shù)家,但我們只有10,500名。其中只有10,000個(gè)是唯一的(例如,有重復(fù)項(xiàng))。
 

  讓我們?cè)陧憫?yīng)對(duì)象列表中查看藝術(shù)家列表的長(zhǎng)度,以了解是否可以更好地了解出了什么問(wèn)題。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  看起來(lái)我們的請(qǐng)求中只有20個(gè)具有響應(yīng)列表-讓我們按順序查看前50個(gè),看看是否有某種模式。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  看起來(lái)在前20個(gè)響應(yīng)之后,此API不會(huì)返回任何數(shù)據(jù)-未記錄的限制。
 

  這不是世界末日,因?yàn)?0,000名藝術(shù)家仍然是大量數(shù)據(jù)。讓我們擺脫前面檢測(cè)到的重復(fù)項(xiàng)。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  使用第二個(gè)Last.fm API端點(diǎn)增強(qiáng)數(shù)據(jù)
 

  為了使我們的數(shù)據(jù)更有趣,讓我們使用另一個(gè)last.fm API端點(diǎn)來(lái)添加有關(guān)每個(gè)藝術(shù)家的一些額外數(shù)據(jù)。
 

  Last.fm允許其用戶創(chuàng)建“標(biāo)簽”以對(duì)藝術(shù)家進(jìn)行分類。通過(guò)使用artist.getTopTags端點(diǎn),我們可以從單個(gè)藝術(shù)家那里獲得頂級(jí)標(biāo)簽。
 

  讓我們以我們的一位藝術(shù)家為例,查看該端點(diǎn)的響應(yīng):

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  {

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

  }

  ]

  }

  }
 

  我們實(shí)際上只對(duì)標(biāo)簽名稱感興趣,然后僅對(duì)最受歡迎的標(biāo)簽感興趣。讓我們使用列表推導(dǎo)來(lái)創(chuàng)建前三個(gè)標(biāo)記名稱的列表:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  然后我們可以使用該str.join()方法將列表轉(zhuǎn)換為字符串:

 

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)


  讓我們創(chuàng)建一個(gè)函數(shù),使用該邏輯為所有藝術(shù)家返回最流行標(biāo)簽的字符串,我們稍后將使用該字符串應(yīng)用于數(shù)據(jù)框中的每一行。
 

  請(qǐng)記住,該函數(shù)將大量連續(xù)使用,因此我們將重用time.sleep()先前的邏輯。


大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)

 

  將此功能應(yīng)用于我們的10,000行將需要不到一個(gè)小時(shí)的時(shí)間。因此,我們知道事情實(shí)際上正在進(jìn)展中,我們將希望像以前一樣監(jiān)視輸出的操作。
 

  不幸的是,在將函數(shù)與pandas Series.apply()方法一起應(yīng)用時(shí),我們不能使用手動(dòng)打印輸出的方法。相反,我們將使用tqdm軟件包來(lái)自動(dòng)執(zhí)行此操作。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  讓我們看一下操作的結(jié)果:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  完成并導(dǎo)出數(shù)據(jù)
 

  在導(dǎo)出數(shù)據(jù)之前,我們可能希望對(duì)數(shù)據(jù)進(jìn)行排序,以便將最受歡迎的藝術(shù)家放在頂部。到目前為止,我們只是將數(shù)據(jù)存儲(chǔ)為文本而不轉(zhuǎn)換任何類型:

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  首先,將偵聽(tīng)器和播放計(jì)數(shù)列轉(zhuǎn)換為數(shù)字。
 

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

  現(xiàn)在,讓我們按聽(tīng)眾數(shù)量排序

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  最后,我們可以將數(shù)據(jù)集導(dǎo)出為CSV文件。

大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)
 

  下一步
 

  在大數(shù)據(jù)分析Python使用Last.fm API獲取音樂(lè)數(shù)據(jù)中,我們學(xué)習(xí)了如何使用大數(shù)據(jù)分析Python來(lái)使用Last.fm API構(gòu)建數(shù)據(jù)集:
 

  a.如何使用API??密鑰通過(guò)API進(jìn)行身份驗(yàn)證

  b.如何使用分頁(yè)從API端點(diǎn)收集更大的響應(yīng)

  c.如何使用速率限制以保持在API準(zhǔn)則之內(nèi)。
 

  如果您想擴(kuò)展學(xué)習(xí)范圍,則可以:
 

  a.完成我們的交互式Dataquest API和抓取過(guò)程,您可以免費(fèi)開(kāi)始。

  b.探索Lasts.fm API中的其他API端點(diǎn)。

  c.嘗試使用此免費(fèi)公共API列表中的一些數(shù)據(jù)。


 

預(yù)約申請(qǐng)免費(fèi)試聽(tīng)課

填寫下面表單即可預(yù)約申請(qǐng)免費(fèi)試聽(tīng)!怕錢不夠?可先就業(yè)掙錢后再付學(xué)費(fèi)! 怕學(xué)不會(huì)?助教全程陪讀,隨時(shí)解惑!擔(dān)心就業(yè)?一地學(xué)習(xí),可推薦就業(yè)!

?2007-2021/北京漫動(dòng)者教育科技有限公司版權(quán)所有
備案號(hào):京ICP備12034770號(hào)

?2007-2022/ 5wd995.cn 北京漫動(dòng)者數(shù)字科技有限公司 備案號(hào): 京ICP備12034770號(hào) 監(jiān)督電話:010-53672995 郵箱:bjaaa@aaaedu.cc

京公網(wǎng)安備 11010802035704號(hào)

網(wǎng)站地圖