LoginSignup
33
23

More than 5 years have passed since last update.

Pythonで短縮URLを生成する【bitlyAPI】

Posted at

初投稿となります。はてなブログから引っ越してきました。

今回は、Pythonを使って短縮URLを生成してみました。bitlyにはモジュールがありますが使えなくなっていたので?自分で作ってみました。大量のURLを捌く時に結構便利ですよ~。

用意するもの&使った環境

・Python 3.6.2 pip3
・requests(pip3 install requestsでインストール)
bitlyでアカウントを作ってアクセストークンを取得しておく

こーど

import requests

def get_shortenURL(longUrl):
    url = 'https://api-ssl.bitly.com/v3/shorten'
    access_token = 'アクセストークン'
    query = {
            'access_token': access_token,
            'longurl':longUrl
            }
    r = requests.get(url,params=query).json()['data']['url']
    return r

実行結果

print(get_shortenURL('https://qiita.com'))

http://bit.ly/2IHRlxO

get_shortenURL()の引数に短縮させたいURLを入れるだけです〜簡単です〜

参考文献

bitly公式API説明書

bitly APIの使い方

Qiitaに投稿する記事の書き方に望むこと
(初投稿なので参考にさせて頂きました!)

33
23
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
33
23