LoginSignup
1
3

More than 5 years have passed since last update.

Python bitly URL短縮

Last updated at Posted at 2018-06-25

Pythonでbitly APIを使ってURLを短縮する例

# bitly APIのtokenと短縮前URL
def createShortURL(access_token, longUrl):
    url = 'https://api-ssl.bitly.com/v3/shorten'
    values = {
            "access_token": access_token,
            "longUrl": longUrl
            }
    response = urllib.request.urlopen(url, urllib.parse.urlencode(values).encode('utf-8'))
    encoding = response.info().get_content_charset('utf-8')
    data = json.loads(response.read().decode(encoding))
    return data["data"]["url"]
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3