Skip to content

Instantly share code, notes, and snippets.

@jackeylu
Created November 24, 2013 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackeylu/7626835 to your computer and use it in GitHub Desktop.
Save jackeylu/7626835 to your computer and use it in GitHub Desktop.
一个url编解码的小程序,通常google搜索结果页面在跳转到真实页面时,需要经过google自身的url,但是有时候会被reset,通过这个代码可以直接获取真实地址。
#!/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