简体   繁体   English

如何使用Python从iTunes URL轻松提取ID

[英]How to easily extract ID from iTunes URL using Python

iTunes URLs look like the following: iTunes网址如下所示:

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? 如何轻松提取ID号?

Example: 例:

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

#returns 472335316
import re

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

I'll let you figure out error handling... 我会告诉你错误处理......

You can use a regex something like "/id(\\\\d+).*" ; 你可以使用像"/id(\\\\d+).*"这样的正则表达式; the first capture group will have the id number in it. 第一个捕获组将包含id号。 I think you can also write it as r"/id(\\d+).*" in Python. 我想你也可以在Python中把它写成r"/id(\\d+).*"

Without regex (for 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM