首页 > 代码库 > 爬虫--用cookie访问任意网页

爬虫--用cookie访问任意网页

#!/usr/bin/env python# -*- coding: utf-8 -*-############################################## File        : requestAnyURLWithCookie.py# Author      : lucasysfeng# Revision    : 2014-06-13 11:08:00# Description : 用cookie访问任何网页(已经有cookie了,不需要登录等验证)#             : cookie用浏览器查看#############################################import cookielibimport urllib2import urllibURL = ""URLCookie = ""### 存储cookiecj = 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)### 访问URLtry:    fileObject = opener.open(URL, postData)except:    print "error: open() error"print fileObject.read()