Skip to content Skip to sidebar Skip to footer

How To Matchobject In Python?

I use Python 2.7. And I want to convert url. What I'd like to is INPUT: HOGEHOGEhttp://super.long.url.comBARBARhttp://super.long.url.com

Solution 1:

Isn't it not well documented on the official documentation?

def make_shorten_url(m):
    long_url = m.group(1)   # Access the capture group here
    import urllib
    import bitly_api
    login_name = 'my id'
    api_key = 'my secret key'
    bitly = bitly_api.Connection(login_name, api_key)
                           # .group() no more needed
    quote_url = urllib.quote(long_url, ':/?&=')
    try:
        result = bitly.shorten(quote_url)
    except:
        return long_url
    return result['url'].encode('utf-8')

Post a Comment for "How To Matchobject In Python?"