爬虫—cookie设置

一、Session模块

#识别人人网中的验证码图片
from lxml import etree
from urllib import request
url = 'http://www.renren.com/'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).text
#解析验证码图片
tree = etree.HTML(page_text)
code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
request.urlretrieve(code_img_url,'./code.jpg')

code_text = getCodeText('bobo328410948','bobo328410948','./code.jpg',2004)

#登录
login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201924831467'
data = {
    "email":"[email protected]",
    "icode":code_text,
    "origURL":"http://www.renren.com/home",
    "domain":"renren.com",
    "key_id":"1",
    "captcha_type":"web_login",
    "password":"436e61246c5c1c36a288c0e625b4e10f9b9c8df72b7a3f6e06a182c38d2286cb",
    "rkey":"77a46f4376c93a80d35930b2cacb6a8d",
    "f":"",
}

#创建一个session对象
session = requests.Session()
#使用session进行请求的发送:获取cookie,且将cookie保存到session中
session.post(url=login_url,data=data,headers=headers)

#获取个人主页对应的页面数据
detail_url = 'http://www.renren.com/289676607/profile'
#该次请求发送是就已经携带了cookie
page_text = session.get(url=detail_url,headers=headers).text
with open('./renren.html','w',encoding='utf-8') as fp:
    fp.write(page_text)

二、古诗词网双层认证

#对古诗文网进行模拟登录
from lxml import etree
from urllib import request
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
session = requests.Session()

#获取验证码图片
url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
page_text = requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
code_img_url = 'https://so.gushiwen.org'+tree.xpath('//*[@id="imgCode"]/@src')[0]
# request.urlretrieve(code_img_url,'./gushiwen.jpg')
img_data = session.get(url=code_img_url,headers=headers).content
with open('./gushiwen.jpg','wb') as fp:
    fp.write(img_data)
    
code_text = getCodeText('bobo328410948','bobo328410948','./gushiwen.jpg',1004)


#执行登录
__VIEWSTATE = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0]
__VIEWSTATEGENERATOR = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0]
login_url = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx'
data = {
    "__VIEWSTATE":__VIEWSTATE,
    "__VIEWSTATEGENERATOR":__VIEWSTATEGENERATOR,
    "from":"http://so.gushiwen.org/user/collect.aspx",
    "email":"[email protected]",
    "pwd":"bobo328410948",
    "code":code_text,
    "denglu":"登录",
}

page_text = session.post(url=login_url,data=data,headers=headers).text

with open('./gushiwen.html','w',encoding='utf-8') as fp:
    fp.write(page_text)

 

你可能感兴趣的:(爬虫)