核心功能 - 长网址变短网址

有时,那些大URL变得非常恼火,很难阅读和共享,此脚可以将长网址变为短网址。

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


import contextlib 


from urllib.parse import urlencode 


from urllib.request import urlopen 


import sys 


 


def make_tiny(url): 


 request_url = ('http://tinyurl.com/api-create.php?' +  


 urlencode({'url':url})) 


 with contextlib.closing(urlopen(request_url)) as response: 


  return response.read().decode('utf-8') 


 


def main(): 


 for tinyurl in map(make_tiny, sys.argv[1:]): 


  print(tinyurl) 


 


if __name__ == '__main__': 


 main() 

这个脚本非常实用,比如说有内容平台是屏蔽公众号文章的,那么就可以把公众号文章的链接变为短链接,然后插入其中,就可以实现绕过。