在Python中通過GET來獲取頁面的COOKIE是非常簡單的事情,下面的代碼實例演示了如何利用Python 獲取COOKIE內容
#! /usr/bin/env python#coding=utf-8# -*-coding:utf-8 -*-#encoding=utf-8import urllibimport urllib2import httplibcj=''header={'Host':'218.94.26.135', 'Accept-Language':'zh-CN', 'Connection':'Keep-Alive', 'Accept-Encoding':'gzip,deflate', 'Accept':'text/html, application/xhtml+xml, */*', 'User-Agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36' }con=httplib.HTTPConnection('218.94.26.135') #建立請求:218.94.26.135/casequery/KT_index.jsp?fydm=320100%20A00con.request(method='GET',url='/casequery/KT_index.jsp?fydm=320100%20A00',headers=header)res=con.getresponse()res.read() #調用read函數以后,才能獲取content。con.close()if res.getheader('Set-Cookie')!=None: #判斷是否存在Set-Cookie,有的話,將cookie保存起來 cj=res.getheader('Set-Cookie').split(';')[0] print cjelse: print 'got no cookie'代碼很簡單,只需要簡單的幾個命令即可獲取
新聞熱點
疑難解答