python爬虫request库中的session

python爬虫request库中的session

一、总结

一句话总结:

The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance, and will use urllib3’s connection pooling.
So if you’re making several requests to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase (see HTTP persistent connection).

二、python爬虫request库中的session

转自或参考:python爬虫(十一) session - 方木Fengl - 博客园
https://www.cnblogs.com/zhaoxinhui/p/12384342.html

这是一个会话对象,对目标服务器得请求通过session来完成

 例如人人网爬取大鹏主页信息,

# requests使用session,不用登录查看人人网大鹏信息

import requests


url='http://www.renren.com/PLogin.do'

id = input('请输入用户名:')
pw = input('请输入密码:')

data = {
        
        "email": id,
        "password": pw}
headers={
    'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
session=requests.session()
session.post(url,data=data,headers=headers)
response=session.get("http://www.renren.com/880151247/profile")
with open('renren.html','w',encoding='utf-8') as fp:
    fp.write(response.text)

在控制台输入用户名和密码之后出来结果:

 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/13266309.html