#!/usr/bin/env python # -*- coding: utf-8 -*- ############################################# # File : requestAnyURLWithCookie.py # Author : lucasysfeng # Revision : 2014-06-13 11:08:00 # Description : 用cookie访问任何网页(已经有cookie了,不需要登录等验证) # : cookie用浏览器查看 ############################################# import cookielib import urllib2 import urllib URL = "" URLCookie = "" ### 存储cookie cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) ### 将cookie放在请求头中 opener.addheaders = [("user-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"), ("cookie", URLCookie)] ### 提交的数据 postValues = {} postData = urllib.urlencode(postValues) ### 访问URL try: fileObject = opener.open(URL, postData) except: print "error: open() error" print fileObject.read()