Created
November 24, 2013 12:37
-
-
Save jackeylu/7626835 to your computer and use it in GitHub Desktop.
一个url编解码的小程序,通常google搜索结果页面在跳转到真实页面时,需要经过google自身的url,但是有时候会被reset,通过这个代码可以直接获取真实地址。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# coding=utf-8 | |
# | |
# a basic url decoder can be helpful to find out the real page link, when you google refered page was blocked by the GFW | |
# | |
from urllib2 import quote, unquote | |
from getopt import getopt, GetoptError | |
import sys | |
def usage(): | |
print "parser.py -e url -d url(encoded)" | |
def main(): | |
try: | |
opts, args = getopt(sys.argv[1:], "e:d:h",["help"]) | |
except GetoptError as err: | |
print str(err) | |
usage() | |
sys.exit(2) | |
#print opts | |
#print args | |
url = None | |
decode = False | |
for o, a in opts: | |
if o == "-d": | |
decode = True | |
url = a | |
elif o == "-e": | |
decode = False | |
url = a | |
elif o in ("-h", "--help"): | |
usage(); | |
sys.exit(0) | |
else: | |
assert False, "unhandled option" | |
if decode: | |
print "Decoding..." | |
s = unquote(url.encode("utf8")) | |
print s | |
print "Splitting with =/&" | |
ss = s.split("&") | |
for i in ss: | |
ii = i.split("=") | |
if len(ii)==2: | |
if ii[0] == "url": | |
print ii[1] | |
print "Done" | |
else: | |
print "Encoding..." | |
print quote(url.encode("utf8")) | |
print "Done" | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment