当前位置: 移动技术网 > IT编程>脚本编程>Python > Python get获取页面cookie代码实例

Python get获取页面cookie代码实例

2018年09月23日  | 移动技术网IT编程  | 我要评论

网上能打电话吗,粉红女王,五福到

在python中通过get来获取页面的cookie是非常简单的事情,下面的代码实例演示了如何利用python 获取cookie内容

#! /usr/bin/env python
#coding=utf-8
# -*-coding:utf-8 -*-
#encoding=utf-8

import urllib
import urllib2
import httplib

cj=''
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%20a00
con.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 cj
else:
  print 'got no cookie'

代码很简单,只需要简单的几个命令即可获取

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网