How to easily extract IDs from iTunes URL using Python

ITunes URLs are as follows:

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 the id number?

Example:

 get_id("http://itunes.apple.com/us/album/brawn/id472335316?ign-mpt=uo") #returns 472335316 
+4
source share
3 answers
 import re def get_id(toParse): return re.search('id(\d+)', toParse).groups()[0] 

I will let you understand the error handling ...

+9
source

You can use a regex like "/id(\\d+).*" ; the first capture group will have an id number in it. I think you can also write it as r"/id(\d+).*" In Python.

+2
source

Without regex (no reason):

 import urlparse def get_id(url): """Extract an integer id from iTunes `url`. Raise ValueError for invalid strings """ parts = urlparse.urlsplit(url) if parts.hostname == 'itunes.apple.com': idstr = parts.path.rpartition('/')[2] # extract 'id123456' if idstr.startswith('id'): try: return int(idstr[2:]) except ValueError: pass raise ValueError("Invalid url: %r" % (url,)) 

Example

 print get_id("http://itunes.apple.com/us/album/brawn/id472335316?ign-mpt=uo") # -> 472335316 
+1
source

Source: https://habr.com/ru/post/1386788/


All Articles