月度归档:2016年08月

python urllib2 请求包含cookie信息

cookie_file = “/tmp/netcraft_cookie.txt”
global_cookie = cookielib.MozillaCookieJar(cookie_file)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(global_cookie))

pq = PyQuery(opener.open(sso_url).read())

csrf_token = pq(“input[name=’csrf_token’]”).attr(“value”)
destination = pq(“input[name=’destination’]”).attr(“value”)
print “get page \n csrf_token : %s \n destination: %s ” % (csrf_token,destination)
global_cookie.save(ignore_discard=True, ignore_expires=True)

python 带有选项的程序

保存如下内容为a.py

from optparse import OptionParser

parser = OptionParser()
parser.add_option(“-f”,”–file”,dest=”keyname”,help=”help to this option”,metavar=”PLACEHOLDER”,default=”default value”)
(options,args) = parser.parse_args()

print options,args

python a.py –help
就可以看到对应的参数