Skip to content

Instantly share code, notes, and snippets.

@hex128
Last active August 26, 2016 20:14
Show Gist options
  • Save hex128/bf389e2c0897abd97295 to your computer and use it in GitHub Desktop.
Save hex128/bf389e2c0897abd97295 to your computer and use it in GitHub Desktop.
Google Play Music Parser
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from json import dumps
from sys import stdout, exit
from codecs import getwriter
from signal import signal, SIGINT
from urllib import urlopen
from bs4 import BeautifulSoup
def parse(html):
result = {}
soup = BeautifulSoup(html, "lxml")
result["url"] = soup.find("meta", {"itemprop": "url"})["content"]
result["name"] = soup.find("div", {"itemprop": "name"}).text.strip()
artist = soup.find("div", {"itemprop": "byArtist"})
result["artist"] = {
"name": artist.find("a", {"itemprop": "name"}).text.strip(),
"url": artist.find("meta", {"itemprop": "url"})["content"]
}
result["genre"] = soup.find("span", {"itemprop": "genre"}).text.strip()
result["price"] = soup.find("div", {"class": "details-info"}).find("button", {"class": "price"}).text.strip()
result["rating"] = soup.find("meta", {"itemprop": "ratingValue"})["content"]
result["rating-count"] = soup.find("meta", {"itemprop": "ratingCount"})["content"]
result["tracks"] = []
for track in soup.find("table", {"class": "track-list"}).find_all("tr", {"class": "track-list-row"}):
result["tracks"].append({
"title": track.find("td", {"class": "title-cell"}).text.strip(),
"duration": track.find("td", {"class": "duration-cell"}).text.strip(),
"price": track.find("td", {"class": "buy-button-cell"}).find("button", {"class": "price"}).text.strip(),
})
return result
def main():
sout = getwriter("utf8")(stdout)
data = parse(urlopen("https://play.google.com/store/music/album?id=B4kqby6eooxlhboaypsl4ahbfau&hl=en").read())
sout.write(dumps(data, ensure_ascii=False, sort_keys=True, indent=2, separators=(',', ': ')) + "\n")
if __name__ == "__main__":
def signal_handler(signal, frame):
exit(0)
signal(SIGINT, signal_handler)
main()
{
"artist": {
"name": "Linkin Park",
"url": "https://play.google.com/store/music/artist/Linkin_Park?id=Ac524cs4icx4xnekce4qkegkrce"
},
"genre": "Metal",
"name": "Meteora",
"price": "UAH137.00",
"rating": "4.8390398025512695",
"rating-count": "5958",
"tracks": [
{
"duration": "0:13",
"price": "UAH22.00",
"title": "Foreword"
},
{
"duration": "3:07",
"price": "UAH22.00",
"title": "Don’t Stay"
},
{
"duration": "3:33",
"price": "UAH22.00",
"title": "Somewhere I Belong"
},
{
"duration": "2:55",
"price": "UAH22.00",
"title": "Lying From You"
},
{
"duration": "2:44",
"price": "UAH22.00",
"title": "Hit the Floor"
},
{
"duration": "3:24",
"price": "UAH22.00",
"title": "Easier to Run"
},
{
"duration": "2:42",
"price": "UAH22.00",
"title": "Faint"
},
{
"duration": "3:17",
"price": "UAH22.00",
"title": "Figure.09"
},
{
"duration": "3:16",
"price": "UAH22.00",
"title": "Breaking the Habit"
},
{
"duration": "2:55",
"price": "UAH22.00",
"title": "From the Inside"
},
{
"duration": "2:58",
"price": "UAH22.00",
"title": "Nobody's Listening"
},
{
"duration": "2:24",
"price": "UAH22.00",
"title": "Session"
},
{
"duration": "3:07",
"price": "UAH22.00",
"title": "Numb"
}
],
"url": "https://play.google.com/store/music/album?id=B4kqby6eooxlhboaypsl4ahbfau"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment