-
짧은 주소(단축URL) 만들기코딩 연습 2022. 6. 21. 21:52
파이썬 pyshorteners 모듈에서 제공하는 짧은 주소(단축URL) 생성 사이트 중에서 가입하지 않아도 되고, API 키를 입력하지 않아도 되는 6개 사이트를 이용해서 단축링크주소를 생성하는 코드를 만들었다.
파이썬 라이브러리로 단축링크주소 만들어보기
[파이썬 소스 코드]
## pip install pyshorteners from pyshorteners import Shortener def shorten_url(link_url): shorteners = [] try: # https://tinyurl.com short_url1 = Shortener().tinyurl.short(link_url) if short_url1: shorteners.append(short_url1) except Exception as e: print(f'Error: {e}') pass try: # https://clck.ru short_url2 = Shortener().clckru.short(link_url) if short_url2: shorteners.append(short_url2) except Exception as e: print(f'Error: {e}') pass try: # https://da.gd short_url3 = Shortener().dagd.short(link_url) if short_url3: shorteners.append(short_url3) except Exception as e: print(f'Error: {e}') pass try: # https://is.gd short_url4 = Shortener().isgd.short(link_url) if short_url4: shorteners.append(short_url4) except Exception as e: print(f'Error: {e}') pass try: # http://osdb.link short_url5 = Shortener().osdb.short(link_url) if short_url5: shorteners.append(short_url5) except Exception as e: print(f'Error: {e}') pass try: # http://chilp.it short_url6 = Shortener().chilpit.short(link_url) if short_url6: shorteners.append(short_url6) except Exception as e: print(f'Error: {e}') pass return shorteners if __name__ == '__main__': link_url = 'https://k-esco.kr/?page_id=17307' shorteners = shorten_url(link_url) for s_url in shorteners: print(s_url)
- Shortener() 객체를 이용하여 입력한 URL에 대해 단축 URL 여러 개를 생성함
- 생성 시 에러가 발생하면 try ~ except 구문으로 에러를 출력하고 다음 사이트로 pass
- 에러가 없으면 생성된 단축 URL 값을 리스트에 저장하여 반환함
[실행 결과]
https://tinyurl.com/232zmwf2 https://clck.ru/rc7yB https://da.gd/pvXW https://is.gd/gRwWBI http://osdb.link/a8j9n http://chilp.it/090e566
'코딩 연습' 카테고리의 다른 글
파이썬 sqlite3 db 활용 대용량 데이터 관리가 가능할까 (0) 2022.06.24 파이썬 장고에서 db table에 직접 접속하는 파일(.py) 만들기 (0) 2022.06.23 파이썬 장고로 주소록 만들기 [장고 기초 예제] (0) 2022.06.17 웹 브라우저 새로고침(F5) 시 데이터 전송 안 되게 하는 문제 (0) 2022.06.16 파이썬 os 모듈(os 또는 os.path)과 pathlib 모듈 비교 (0) 2022.06.06