1. cookie和session区别

python爬虫笔记(三)requests模块深入—模拟登录的三种方式_post请求

2. 爬虫处理cookie和session

python爬虫笔记(三)requests模块深入—模拟登录的三种方式_safari_02

3. 处理cookies和session请求

python爬虫笔记(三)requests模块深入—模拟登录的三种方式_post请求_03

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_验证码_04

 4. 尝试使用session登录人人网(别试,了解一下)
# -*- coding: utf-8 -*-

import requests

session = requests.session()

post_url = "http://www.renren.com/PLogin.do"
post_data = {"email":"mr_mao_hacker@163.com", "password":"alarmchime"}

headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
        }

# 使用session发送post请求,cookie保存在其中
session.post(post_url, data=post_data, headers=headers)

# 在使用session进行请求登录之后才能访问
r = session.get("http://www.renren.com/327550029/profile", headers=headers)

print(r.request.url)

# 保存页面
with open("reren1.html", "w", encoding="utf-8") as f:
    f.write(r.content.decode())

现在好像要验证码了。。。。。

python爬虫笔记(三)requests模块深入—模拟登录的三种方式_chrome_05

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_html_06

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_chrome_07

5. 字典推导式和列表推导式

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_post请求_08

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_html_09

{i.split("=")[0]:i.split("=")[1] for i in cookie.split("; ")}

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_safari_10

 python爬虫笔记(三)requests模块深入—模拟登录的三种方式_chrome_11