Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily extract ID from iTunes URL using Python

Tags:

python

regex

iTunes URLs look like the following:

http://itunes.apple.com/us/album/break-of-dawn/id472335316?ign-mpt=uo%3D
http://itunes.apple.com/us/app/monopoly-here-now-the-world/id299110947?mt=8
http://itunes.apple.com/es/app/revista-/id397781759?mt=8%3Futm_so%3Dtwitter
http://itunes.apple.com/app/id426698291&mt=8"
http://itunes.apple.com/us/album/respect-the-bull-single/id4899
http://itunes.apple.com/us/album/id6655669

How can I easily extract id number?

Example:

get_id("http://itunes.apple.com/us/album/brawn/id472335316?ign-mpt=uo")

#returns 472335316
like image 741
Sam Avatar asked Dec 17 '22 06:12

Sam


1 Answers

import re

def get_id(toParse):
    return re.search('id(\d+)', toParse).groups()[0]

I'll let you figure out error handling...

like image 177
odgrim Avatar answered Dec 18 '22 19:12

odgrim