python 爬虫cookie的简单使用

来源:互联网 发布:电脑怎样编程软件 编辑:程序博客网 时间:2024/05/21 06:25

#coding=utf-8

 

import urllib

import urllib2

import cookielib

from bs4 import BeautifulSoup

 

url ="http://www.baidu.com"

values = {

            'userName':'aaaaaa',

            'password':'bbbbbb'

         }

postdata =urllib.urlencode(values)

user_agent = "Mozilla/5.0(Windows NT 6.1; WOW64)"

headers ={"User-Agent":user_agent}


#以下为创建opener

file_name = 'cookie.txt'

cookie =cookielib.MozillaCookieJar(file_name)

handler =urllib2.HTTPCookieProcessor(cookie)

opener =urllib2.build_opener(handler)

 

#请求url

try:

    request = urllib2.Request(url, data = None,headers=headers)

    response = opener.open(request, timeout =2)

except urllib2.HTTPError, e:

    print e.code

except urllib2.URLError, e:

    print e.reason

except:

    print "Error"

#保存cookie到文件中

cookie.save(ignore_discard=True,ignore_expires=True)   

data = response.read()

soup = BeautifulSoup(data,"lxml")

for link in soup.find_all('a'):

    print link


更多详细内容请参考 http://cuiqingcai.com/1052.html


1 0
原创粉丝点击